Why is this Fibonacci code not correct? [closed]

五迷三道 提交于 2019-12-13 04:33:47

问题


Project Euler Question 2.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

My solution :

int firstNum = 1;
int secondNum = 2;
int resultNum = firstNum + secondNum;
int sum = 0;

for (int i = 0; i < 4000000; i++)
{
    firstNum = i;
    secondNum = i;

    if(resultNum == firstNum + secondNum)
    {
        sum += resultNum;
        Console.WriteLine(sum);
    }
}

Why is this not correct and can you guide me into the right way of thinking?


回答1:


Try this:

    int n1, n2, fib;
        //n1 = 0;
        //n2 = 1;
        n1 = 1;
        n2 = 1;

        fib = n1 + n2;

        while (fib < 4000000)
        {

            n2 = n1;
            n1 = fib;
            fib = n1 + n2;
        }

Then find the even fib numbers and sum it




回答2:


For a more modular approach (mixed with LINQ):

IEnumerable<Int32> Fibonacci(Int32 limit = 4000000)
{
    for (Int32 previous = 0, current = 1, next = 0; 
        current <= limit; current = next)
    {
        next = previous + current;
        previous = current;
        yield return next;
    }
}

Then:

var allNumbers = Fibonacci(4000000); // 1,2,3,5,8,13,21
var evenNumbers = allNumbers.Where(x => x % 2 == 0); // 2,8,34,144,610,2584
var sum = evenNumbers.Sum(); // 4613732



回答3:


The fibonacci series is defined as

A0 = 1, A1 = 1

An = An-1 + An-2

You are aiming at producing the pattern

1 2 3 5 8 13 etc

While iterating, you are going to want to adjust the input similar to a sliding window and then check to see if you have come across a valid insertion (i.e. < 4M and even)

int sum = 0;
int max = 4000000;
for( int n = 0; n < max ; n++ )
{
 //only sum the even numbers
 if( second % 2 == 0 ) sum += second; 

 //adjust
 int result = first + second;
 first = second;
 second = result;

 //test for numbers greater than max
 if( result > max ) break;
}

//output
Console.WriteLine(sum); //An for all even An values

After looking at this hopefully you can see some of the issues you came across.

You are setting your variables to the iterator i which is not going to produce An as defined but instead something entirely different.

firstNum = i;
secondNum = i;

Further, you only calculate the result once. This needs to be done in the loop. Only calculating once will basically use a static value the entire time.

int resultNum = firstNum + secondNum;

The conditional statement should be testing for an even number in order to properly add to the sum, but this code will only test the static value of resultNum

if(resultNum == firstNum + secondNum)

Also, there needs to be some check on the sum in order to break out when the max is exceeded. 4M iterations will be too many.


There is even more optimization that can occur here though. Looking at the for loop, it is clear that while not used yet, the iterator can be a powerful tool.

The reason being that the fibonacci conforms to the "Golden ratio".

By making the simple observation that the fibonacci series hits an even number ever 3 iterations, the iterator can be used to skip through the series.

double p = (1 + Math.Pow(5,.5)) / 2;
for( int n = 3, sum = 0;;n+=3)
{
 double f = ( Math.Pow(p,n) - Math.Pow( 1 - p , n ) ) / Math.Pow(5,.5);
 if( f > 4000000 ){ 
  Console.WriteLine(sum);
  break;
 }
 sum += (int)Math.Round(f);
}



回答4:


Your code does not produce a Fibonacci sequence and does no check for even-valued terms

Try this instead

int firstNum = 1;
int secondNum = 2;

int sum = 0;

while (secondNum <= 4000000)
{
    if (secondNum % 2 == 0)
        sum += secondNum;

    int resultNum = firstNum + secondNum;
    firstNum = secondNum;
    secondNum = resultNum;
}

Console.WriteLine(sum);


来源:https://stackoverflow.com/questions/19252495/why-is-this-fibonacci-code-not-correct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!