Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace AlgorithmsOverStructureData
- {
- class Program
- {
- static Dictionary<int> resultHistory = new Dictionary<int>();
- static void Main(string[] args)
- {
- Console.WriteLine(Factorial(5)); //5! = 5 * 4 * 3 * 2 * 1
- Console.WriteLine(FibonacciRecursiveOpt(6));
- }
- static long Factorial(int num)
- {
- if (num == 0)
- {
- return 1;
- }
- return num * Factorial(num - 1);
- }
- static int FibonacciRecursiveOpt(int n)
- {
- if (n == 0) return 0;
- if (n == 1) return 1;
- if (resultHistory.ContainsKey(n))
- return resultHistory[n];
- int result = FibonacciRecursiveOpt(n - 1) + FibonacciRecursiveOpt(n - 2);
- resultHistory[n] = result;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement