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

后端 未结 9 2273
后悔当初
后悔当初 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:56
         // count(user input) of Fibonacci numbers   
            int[] array = new int[20];
            array[0] = 0;
            array[1] = 1;
    
            Console.WriteLine(array[0] + "\n" + array[1]); 
    
            for (int i = 2; i < 20; i++)
            {
                array[i] = array[i - 1] + array[i - 2];
                Console.WriteLine(array[i]); 
            }
    
    0 讨论(0)
  • 2021-02-06 19:57

    I think the question is written to say that you would add all the even numbers together while the numbers in the sequence don't exceed four million, meaning you would add 3,999,992.

    0 讨论(0)
  • 2021-02-06 19:57

    Here's a nice way to find Fibonnaci numbers.

    IEnumerable<BigInteger> Fibs()
    {
        for(BigInteger a = 0,b = 1;;b = a + (a = b))
            yield return b;
    }
    
    0 讨论(0)
提交回复
热议问题