Advertisement
yanass

Recursive Fibonacci

Jun 4th, 2019
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace fibonacci_recursive
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int nFibonacci = int.Parse(Console.ReadLine());
  11.  
  12.             if (nFibonacci <= 1)
  13.             {
  14.                 Console.WriteLine(1);
  15.             }
  16.  
  17.             else
  18.             {
  19.  
  20.                 double plusPhi = (1 + Math.Sqrt(5)) / 2;
  21.                 double minusPhi = (1 - Math.Sqrt(5)) / 2;
  22.  
  23.  
  24.                 double fibonacci = (Math.Pow(plusPhi, nFibonacci) - Math.Pow((-minusPhi), nFibonacci))
  25.                     / Math.Sqrt(5);
  26.  
  27.                 long roundedFib = (long)Math.Round(fibonacci);
  28.  
  29.                 Console.WriteLine(roundedFib);
  30.  
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement