欧拉计划 2

Euler Project 2

所以我还不是很擅长(轻描淡写)。我正在尝试解决Euler项目中的问题,而我已经卡在了2.

Each new term in the Fibonacci sequence is generated by adding the previous 2 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.

这是我反复尝试修复的代码: (我认为for循环逻辑有问题。)

public class tesy {
    public static void main(String args[]) {
        int fib = 0;
        int tot = 0;
        int total = 0;

        for (fib = 0; tot < 4000000; fib++) {
            tot = fib + (fib + 1);

            if (tot % 2 == 0) {
                total = tot + total;
            }
        }
        System.out.println(total);
    }
}

您似乎没有遵循用于生成斐波那契数列的实际方程,因此没有(明显的)方法来修复您的代码。

int fibA = 1, fibB = 2, total = 0;

while(fibB <= 4000000) {
    // Add to the total, set fibA to fibB and get the next value in the sequence.
    if(fibB % 2 == 0) total += fibB;
    int temp = fibA;
    fibA = fibB;
    fibB = fibB + temp;
}

上面的代码应该找到小于或等于 4000000 的所有值的总和

你的逻辑在几个方面是错误的,

tot = fib + (fib + 1); /** This will always be `(2*fib + 1)` and `fib` is getting 
incremented by 1 each time. You have no reference to the previous two terms of the 
sequence. **/

试试下面的逻辑。

class Fibonacci
{
    public static void main (String[] args)
    {
        int fiboFirst = 1;
        int fiboSecond =2;
        int fib = 0;
        int sum = 0;

        while(fiboSecond < 4000000)
          {
            // This will calculate the current term of the sequence
            fib = fiboFirst + fiboSecond;  

            // Below two lines will update fib[i] and fib[i - 1] terms
            // for the next loop iteration.
            fiboFirst = fiboSecond;   // fib[i]
            fiboSecond = fib;   // fib[i -1]
            if (fib % 2 == 0)
              {
                sum = sum + fib;
              }
          }
        System.out.println(sum+2);
    }
}

Explanation

Here fiboFirst is equivalent to F[n] and fiboSecond is equivalent to F[n - 1] in the Fibonacci sequence definition. In each iteration, those two values should be replaced, in order to be used in the next iteration. That is why I have these two lines,

fiboFirst = fiboSecond;   // fib[i]
fiboSecond = fib;   // fib[i -1]

HERE就是上面程序的执行

这是一个使用 BigInteger 的解决方案。请验证结果。

public class Fibonacci{

    public static void main(String[] args) {
        BigInteger r = fibonacciEvenSum();
        System.out.println(r);
    }

    public static BigInteger fibonacciEvenSum(){
        int f = 1;
        int s = 2;
        int mn4 = 4000000;
        BigInteger sum = BigInteger.valueOf(0);

        while(s <= mn4){
            if(s % 2 == 0){
                sum = sum.add(BigInteger.valueOf(s));
            }
            f = f + s;
            s = s + f;
        }
        return sum;
    }

}

在编写这样的程序之前,您应该首先考虑这个程序的底层是什么。您应该首先了解如何生成斐波那契数列,然后再开始使用该数列做一些事情。我给你我的解决方案,让你明白。

class euler2 {
    public static void main(String[] args) {
        int a = 0, b = 1; /* the first elements of Fibonacci series are generally
                          thought to be 0 and 1. Therefore the series is 0, 1, 1, 2, 3... . 
                          I've initialized first and second elements such  */
        double sum = 0; // The initial sum is zero of course.
        while (b < 4000000) /* since b is the second term, it will be our control variable. 
                            This wouldn't let us consider values above 4M. */
        {
            int ob = b; // to swap the values of a and b.
            b = a + b; // generating next in the series.
            a = ob; // a is now the older value of b since b is now a + b.
            if (b % 2 == 0) // if b is even
                sum += b; // we add it to the sum
        }
        System.out.println(sum); // and now we just print the sum
    }
}

希望对您有所帮助!