Advertisement
ksmk99

1000-digit Fibonacci number №25

Sep 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. //The Fibonacci sequence is defined by the recurrence relation:
  8.  
  9. //Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
  10. //Hence the first 12 terms will be:
  11.  
  12. //F1 = 1
  13. //F2 = 1
  14. //F3 = 2
  15. //F4 = 3
  16. //F5 = 5
  17. //F6 = 8
  18. //F7 = 13
  19. //F8 = 21
  20. //F9 = 34
  21. //F10 = 55
  22. //F11 = 89
  23. //F12 = 144
  24. //The 12th term, F12, is the first term to contain three digits.
  25.  
  26. //What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
  27. namespace _25
  28. {
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             BigInteger max = BigInteger.Pow(10, 999);
  34.             BigInteger item1 = 1;
  35.             BigInteger item2 = 1;
  36.             BigInteger sum = 0;
  37.             BigInteger count = 2;
  38.             while(item1 < max)
  39.             {
  40.                 sum = item1 + item2;
  41.                 item2 = item1;
  42.                 item1 = sum;
  43.                 count++;
  44.             }
  45.             Console.WriteLine(count);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement