# BigInteger laziness

Published on 2011-07-28I believe I was meant to solve problem 25 with byte arrays or some other kind of fancy large integer arithmetic, but .Net's `BigInteger`

class is, of course, very fast and easy to use.

Problem 25 reads:

The Fibonacci sequence is defined by the recurrence relation:

F

_{ n}= F_{ n-1}+ F_{ n-2}, where F_{1}= 1 and F_{2}= 1.

Hence the first 12 terms will be:

F

_{1}= 1 F_{2}= 1 F_{3}= 2 F_{4}= 3 F_{5}= 5 F_{6}= 8 F_{7}= 13 F_{8}= 21 F_{9}= 34 F_{10}= 55 F_{11}= 89 F_{12}= 144

The 12th term, F

_{12}, is the first term to contain three digits.

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

My solution was to simply calculate Fibonacci numbers until one was 1,000-digits long.

using System; using System.Numerics; using System.Collections.Generic; using System.Linq; using System.Text; namespace Problem025 { class Program { static void Main(string[] args) { BigInteger fib1 = new BigInteger(1); BigInteger fib2 = new BigInteger(1); BigInteger nextFib = new BigInteger(0); int fibSeqNum = 2; while (nextFib.ToString().Length < 1000) { nextFib = fib1 + fib2; fib1 = fib2; fib2 = nextFib; fibSeqNum++; } Console.WriteLine(fibSeqNum); } } }