Advertisement
AndrianaTodorova

Untitled

Jan 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _08.RecursiveFibonacci
  8. {
  9.     class Program
  10.     {
  11.         static long[] memoization;
  12.         static void Main(string[] args)
  13.         {
  14.             long n = long.Parse(Console.ReadLine());
  15.             memoization = new long[n];
  16.             Console.WriteLine(getFibonacci(n));
  17.         }
  18.         static long getFibonacci(long n)
  19.         {
  20.             if (memoization[n - 1] == 0)
  21.             {
  22.                 if (n <= 2)
  23.                 {
  24.                     memoization[n - 1] = 1;
  25.                 }
  26.                 else
  27.                 {
  28.                     memoization[n - 1] = getFibonacci(n - 1) + getFibonacci(n - 2);
  29.                 }
  30.             }
  31.  
  32.             return memoization[n - 1];
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement