I can't understand this Fibonacci stream

so I’m new to the world of programming and thought, I’ll take a book to start learning. I bought the A Players Guide for the C # 3rd Edition and one of the small homework tasks he gives you is very strong. I debugged it step by step to help me understand, but the program flow makes no sense to me. Here it is.

      static void Main(string[] args)
        {
            for (int index = 1; index <= 10; index++)
            {
                Console.WriteLine(Fibonacci(index));
            }

            Console.ReadKey();
        }

        /// <summary>
        /// Returns a number from the Fibonacci sequence, starting at 1.
        /// Note that this implementation is not very optimized, and can
        /// take a very long time if you're looking up large numbers.
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        static ulong Fibonacci(int number)
        {
            if (number == 1) { return 1; }
            if (number == 2) { return 1; }

            return Fibonacci(number - 1) + Fibonacci(number - 2);
        }

for, "1" "1", "1" , true, "2" ' if . 3- , 3, return, (3-1) + (3-2) 3, . , return 3 , . , , 2. (ok?), , 2- "1". , , return. , , ? , .

+4
2

.

,

, :

return Fibonacci(number - 2) + Fibonacci(number - 1);

, , .

Recursive Fibonacci Challenges

: http://composingprograms.com/pages/28-efficiency.html

:

, fib, 6, fib (4), ( ), , , fib (5) ( ).

, . , .

, fib (n) n.

O (2 ^ n),

, ! fib (n), n.

, , . O (n * 2 ^ n)

+8

, , , ... , , .

, , , ( , , ).

( , , , 5!= 5 * 4 * 3 * 2 * 1).

, - :

Int64 Factorial(int input)
{
    if(input < 0) 
    {
         throw new ArgumentOutOfRangeException("Input must be 0 or higher.");
    }
    if(input < 2) 
    {
        return 1;
    }
    return input * Factorial(input - 1);
}

(0!= 1 -3! ...)

, 3:
Factorial(3) 3 * Factorial(3-1).
, Factorial(2) 2 * Factorial(2-1).
Factorial(1) 1.
Factorial(3) 3 * 2 * 1, 3!.

Fibonacci - 1 2, : Fibonacci(number - 1) + Fibonacci(number - 2);. for, ( 1 10).

, Fibonacci(3):

Fibonacci(3) Fibonacci(3-1) + Fibonacci(3-2).
Fibonacci(2) 1.
Fibonacci(1) 1.

, Fibonacci(3) 1 + 1.

:

Fibonacci(4) Fibonacci(4-1) + Fibonacci(4-2).
, Fibonacci(3) 2, , Fibonacci(2) 1, Fibonacci(4) 3
( 1 + 1 + 1, ).

, Fibonacci(5) Fibonacci(5-1) + Fibonacci(5-2) - , 3 + 2 .. (Fibonacci(6) 5 + 3, Fibonacci(7) 8 + 5).

+3

All Articles