Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class MainClass
- {
- public static void Main()
- {
- Console.Write("Enter n = ");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine(Solve(n));
- Console.WriteLine(SolveRecursevely(n));
- Console.WriteLine(SolveMemoization(n));
- }
- public static double Solve(int n)
- {
- double n1 = 4 / 3.0;
- double n2 = 3;
- double n3 = 4;
- if (n == 1) return n1;
- if (n == 2) return n2;
- if (n == 3) return n3;
- double nth = 0;
- for (int i = 4; i <= n; i++)
- {
- nth = n3 + 2 * n2 - 8 * n1;
- n1 = n2;
- n2 = n3;
- n3 = nth;
- }
- return nth;
- }
- public static double SolveRecursevely(int n)
- {
- if (n == 1) return 4 / 3.0;
- if (n == 2) return 3;
- if (n == 3) return 4;
- return SolveRecursevely(n - 1) + 2 * SolveRecursevely(n - 2) - 8 * SolveRecursevely(n - 3);
- }
- public static double SolveMemoization(int n)
- {
- double result;
- if (memo.ContainsKey(n))
- {
- result = memo[n];
- }
- else
- {
- result = SolveMemoization(n - 1) + 2 * SolveMemoization(n - 2) - 8 * SolveMemoization(n - 3);
- memo[n] = result;
- }
- return result;
- }
- static Dictionary<int, double> memo = new Dictionary<int, double>() {
- {1, 4 / 3.0},
- {2, 3},
- {3, 4}
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement