using System;
using System.Collections.Generic;
class MainClass
{
static void Main()
{
// Given a number N, return the index value of the Fibonacci sequence
// prev 2 numbers = next number
// 0,1,1,2,3,5,8,13,21,34,55,89,144
// e.g. input is 3 so output is 2 OR input is 6 and output is 8
// SO input number is the index
Console.WriteLine($"Iterative: {FibonacciIterative(6)}");
Console.WriteLine($"Recursive: {FibonacciRecursive(6)}");
}
static int FibonacciIterative(int number)
{
List<int> fibonacci = new List<int>() { 0, 1 };
for (int i = 2; i < number + 1; i++)
{
fibonacci.Add(fibonacci[i - 1] + fibonacci[i - 2]); // NOT i=5 so i-1 = 4 BUT if i=5 the index one BEFORE this is 4 and it's value is 3
// SO if i = 5, 5-1=4 index (value=3) & 5-2=3 index (value=2), 3+2 = 5 (value to input at the current end of the list)
}
return fibonacci[number];
}
static int FibonacciRecursive(int number)
{
if (number < 2) { return number; }
return FibonacciRecursive(number - 1) + FibonacciRecursive(number - 2);
}
// start at 2 because if index is 0 the answer is 0, if it's 1 answer is 1 and if it's 2 the answer 1
// the method itself doesn't do any workings out!
// the previous number before the one given in the input (n-1) and the one before that (n-2) always equal the given number!
}