Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace N_th_member_of_a_number
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Please enter the number: ");
- int a = int.Parse(Console.ReadLine());
- int iterativeProduct = iterativeVersion(a);
- static int iterativeVersion(int a)
- {
- int a1 = 2;
- int a2 = 4;
- int a3 = 6;
- int result = 0;
- if (a == 1)
- return 2;
- if (a == 2)
- return 4;
- if (a == 3)
- return 6;
- for (int i = 4; i <= a; i++)
- {
- result = 3 * a1 + 4 * a2 - 7 * a3;
- a1 = a2;
- a2 = a3;
- a3 = result;
- }
- return result;
- }
- Console.WriteLine($"Iterative result is: {iterativeProduct}");
- Console.WriteLine();
- int recursiveProduct = recursiveVersion(a);
- static int recursiveVersion(int a)
- {
- if (a == 1)
- return 2;
- if (a == 2)
- return 4;
- if (a == 3)
- return 6;
- return 3 * recursiveVersion(a - 3) + 4 * recursiveVersion(a - 2) - 7 * recursiveVersion(a - 1);
- }
- Console.WriteLine($"Recursive result is: {recursiveProduct}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement