欧拉项目2

时间:2015-08-08 16:04:57

标签: java project fibonacci

所以我一点也不擅长(轻描淡写)。我正在尝试解决Euler项目中的问题,我已经陷入了困境。

  

Fibonacci序列中的每个新术语都是通过添加前两个术语生成的。从1和2开始,前10个术语将是:

     

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

     

通过考虑Fibonacci序列中的值不超过四百万的项,找到偶数项的总和。

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

@api.onchange('state')
def log_prod(self,state):
    if state in ["i"]:
       msg="Dear User: your account has been update"
       return self.message_post(body=msg)

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

你的逻辑在某些方面是错误的,

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);
    }
}
  

<强>解释

     

此处fiboFirst相当于F [n],fiboSecond等效   到Fibonacci序列定义中的F [n - 1]。在每次迭代中,   应该替换这两个值,以便在下一个中使用   迭代。这就是为什么我有这两行,

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

HERE是上述程序的执行

答案 2 :(得分:0)

这是一个使用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;
    }

}

答案 3 :(得分:0)

在编写这样的程序之前,首先应该考虑这个程序的基础。你应该首先了解如何在毕业前对系列做一些事情来生成一个斐波纳契系列。我会给你解决方案,以便你能理解。

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
    }
}

希望这有帮助!