Finding Fibonacci sequence in C#. [Project Euler Exercise]

后端 未结 9 2289
后悔当初
后悔当初 2021-02-06 19:22

I\'m having some trouble with this problem in Project Euler.

Here\'s what the question asks:

Each new term in the Fibonacci sequence is generated by adding t

9条回答
  •  深忆病人
    2021-02-06 19:53

    Joel, I wrote a very some similiar code; I'm posting it anyways:

    static IEnumerable Fibonacci(int maximum)
    {
        int auxiliar = 0;
        int previous = 0;
        int current = 1;
        while (current < maximum)
        {
            auxiliar = previous;
            previous = current;
            current = auxiliar + current;
            yield return current;
        }
    }
    
    Console.WriteLine(Fibonacci(4000000).Where(number => number % 2 == 0).Sum());
    

提交回复
热议问题