【问题标题】:Euler Project 2欧拉计划 2
【发布时间】:2015-09-29 01:59:11
【问题描述】:

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

斐波那契数列中的每个新项都是通过添加前 2 个项生成的。从 1 和 2 开始,前 10 个术语将是:

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

通过考虑斐波那契数列中值不超过四百万的项,求偶数项之和。

这是我反复尝试修复的代码: (我认为 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);
    }
}

【问题讨论】:

  • 对每个阅读您的代码的人来说非常重要的一件事是格式化(尤其是缩进)。如果您使用 IDE(例如 IntelliJ IDEA),这将自动为您完成。
  • 您无需为您的年龄或您目前的 Java 知识道歉 :)。但是你能解释一下你当前的代码有什么问题吗?
  • 你为什么不遵循斐波那契数列的定义呢?您正在添加fib + (fib+1),它只是2*fib + 1,而不是记住之前的斐波那契数(不是它在序列中的位置!)并将其添加到当前的斐波那契数中以检索您的新斐波那契数。
  • tot = fib + (fib + 1) 计算“'fib' 的前一个值与 'fib' 的前一个值加一的和”。你想要“前两个斐波那契数的总和”。 (提示:您需要两个变量来对这样的序列求和 - 前一项和之前的项......)

标签: java project fibonacci


【解决方案1】:

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

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等价于 到斐波那契数列定义中的 F[n - 1]。在每次迭代中, 这两个值应该被替换,以便在下一个使用 迭代。这就是为什么我有这两行,

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

HERE是上面程序的执行

【讨论】:

  • 逻辑看起来非常好,比我自己的要好得多,但有些我不明白 - 例如 fiboSecond = fib; - 你能评论一下代码吗?为什么每一行都在那里?谢谢
  • 啊!现在我明白了——它是如此清晰。谢谢和很棒的代码顺便说一句。
  • 谢谢! :) 我也用解释编辑了我的答案。希望能帮助到你。如果我的回答解决了您的问题,您也可以接受...
  • 接受并编辑代码 - 感谢大家解决问题。
【解决方案2】:

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

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 的值的总和

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      在编写这样的程序之前,您应该首先考虑这个程序的基础。您应该首先了解如何生成斐波那契数列,然后再继续使用该数列。我会给你我的解决方案,以便你理解。

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

      希望这有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多