Project Euler

Project Euler Problem 25

steloflute 2012. 6. 8. 23:05

Problem 25

30 August 2002

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?


Answer:
4782

 

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.IO;

namespace Euler {
    class Program {
        static void Main(string[] args) {
            BigInteger f = 1;
            BigInteger a = 1;
            BigInteger b = 1;
            int i = 3;
            for (i = 3; ; i++) {
                f = a + b;
                a = b;
                b = f;
                if (f.ToString().Length >= 1000) break;                
            }
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}




Racket
(let loop ([a 1]
           [b 1]
           [index 1])
  (if (>= (string-length (number->string a)) 1000)
      (displayln index)
      (loop b (+ a b) (add1 index))))

'Project Euler' 카테고리의 다른 글

Project Euler Problem 27  (0) 2012.06.08
Project Euler Problem 26  (0) 2012.06.08
Project Euler Problem 24  (0) 2012.06.08
Project Euler Solutions in Clojure: clojure-euler  (0) 2012.06.05
Project Euler Problem 23  (0) 2012.06.03