c#

25 - 1000_digit_fibonacci_number

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, 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 index of the first term in the Fibonacci sequence to contain 1000 digits?

cevap : 4782

link

Derleme ortami Visual studio community c# bignum large numbers

// project euler problem 25
// yasin tasan --> apr 2018
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            UInt32 index = 1, digit = 0;
            BigInteger sum = 0, first = 1, second = 1;

            Console.WriteLine("F_{0} = 1\n",index);
            Console.WriteLine("digit = 1\n\n");

            Console.WriteLine("F_{0} = 1\n",index++);      
            Console.WriteLine("digit = 1\n\n");      

            sum = first + second;
            Console.WriteLine("F_{0} = {1}\n",index++,sum);      
            Console.WriteLine("digit = 1\n\n");      

            while (digit < 1000)
            {
                index++;
                first = second;
                second = sum;
                sum = first + second;

                Console.WriteLine("F_{0} = {1}\n",index, sum);      

                check_digit(sum, ref digit);
                Console.WriteLine("digit = {0}\n\n",digit);      
            }     
        }
        static void check_digit(BigInteger sum, ref UInt32 digit)
        {
            digit = 0;
            while (sum > 0)
            {
                sum = sum / 10;
                digit++;
            }
        }
    }
}