Esempio n. 1
0
        /* Write a function that takes a int arg and returing the n-th value on
         *  fibonacci sequence. Example:
         *  n:      1   2   3   4   5   6   7   8
         *  fib(n): 1   1   2   3   5   8   13  21
         */
        static void Main(string[] args)
        {
            Fibondetails.detailsOn();

            Console.WriteLine(fib(7));

            Fibondetails.endstats();
        }
        // Use simple recursive.
        // Big O: Time O(exponential)
        //          Space O(N)
        static public int fib(int n)
        {
            Fibondetails.stat(n);

            if (n <= 2)
            {
                Fibondetails.returnval(1);
                return(1);
            }
            int sum = fib(n - 1) + fib(n - 2);

            Fibondetails.returnsum(sum);
            return(sum);
        }