项目欧拉,问题2- Java

时间:2011-05-16 00:34:20

标签: java

我是一个相对较新的java程序员,我现在一直在修补这个程序,现在我已经陷入困境了。我希望你能帮助我。

因此该程序应符合以下要求:

  

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

     

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

     

考虑中的条款   Fibonacci序列,其值不是   超过四百万,找到的总和   有价值的条款。

这是我的代码:

    //Generates Fibonacci sequence
    while (fibNum < 144)
    {
        int lastValue = (Integer) fibList.get(fibList.size()-1);
        int secondToLastValue = (Integer) fibList.get(fibList.size()-2);

        fibNum = secondToLastValue + lastValue;

        if (fibNum < 144)
        {
            fibList.add(fibNum);
        }

    //Picks out the even numbers from limitFibList
    for (int i = 0; i < fibList.size(); i++)
    {
        if ((Integer) fibList.get(i) % 2 == 0)
        {
            evenNumsFibList.add(fibList.get(i));
        }
    }

    //Sums up the total value of the numbers in the evenNumsFibList
    for (int i = 0; i < evenNumsFibList.size(); i++)
    {
        sum += (Integer) evenNumsFibList.get(i); 
    }

...这是我得到的输出:

Fibonacci sequence list: [1, 2, 3]
Size of the Fibonacci list: 3
Even Numbers list: [2]
Total sum of even numbers: 2

Fibonacci sequence list: [1, 2, 3, 5]
Size of the Fibonacci list: 4
Even Numbers list: [2, 2]
Total sum of even numbers: 6

Fibonacci sequence list: [1, 2, 3, 5, 8]
Size of the Fibonacci list: 5
Even Numbers list: [2, 2, 2, 8]
Total sum of even numbers: 20

Fibonacci sequence list: [1, 2, 3, 5, 8, 13]
Size of the Fibonacci list: 6
Even Numbers list: [2, 2, 2, 8, 2, 8]
Total sum of even numbers: 44

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21]
Size of the Fibonacci list: 7
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8]
Total sum of even numbers: 78

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34]
Size of the Fibonacci list: 8
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34]
Total sum of even numbers: 156

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55]
Size of the Fibonacci list: 9
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 278

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 444

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 654

显然我的while循环导致了我的问题,但我不知道如何解决它。

非常感谢您的帮助,

哈克

5 个答案:

答案 0 :(得分:8)

如果你仔细看一下你实际需要的Fibonacci序列中的数字(只有偶数需要求和),你会看到一个模式:

0  1  1  2  3  5  8  13  21  34  55  89  144 ...
-  O  O  E  O  O  E   O   O   E   O   O    E

请注意,从0开始的每个第3个数字都是偶数。因此,如果计算每第三个Fibonacci数,则可以消除任何均匀性检查。再看一下序列,您可以看到,如果k是您正在查看的当前甚至Fibonacci数,并且j是前一个,则下一个偶数Fibonacci数n可以是获得者:

n = 4k + j

所以在Java中,你可以尝试这样的事情:

int j = 0;
int k = 2;
int sum = j+k;

while (k < LIMIT) {
    int tmp = 4*k + j;
    sum = sum + tmp;
    j = k;
    k = tmp;
}

答案 1 :(得分:5)

看起来你错过了while循环的近括号。所以其他for正在其中运行。

所以:

while (fibNum < 144)
    {
        int lastValue = (Integer) fibList.get(fibList.size()-1);
        int secondToLastValue = (Integer) fibList.get(fibList.size()-2);

        fibNum = secondToLastValue + lastValue;

        if (fibNum < 144)
        {
            fibList.add(fibNum);
        }
    }

    //Picks out the even numbers from limitFibList
    for (int i = 0; i < fibList.size(); i++)
    {...

答案 2 :(得分:0)

问题在于:

for (int i = 0; i < fibList.size(); i++)
{
    if ((Integer) fibList.get(i) % 2 == 0)
    {
        evenNumsFibList.add(fibList.get(i)); <-- HERE
    }
}

您正在将所有偶数的全新列表添加到已有列表的末尾。

在再次调用此循环之前,您需要删除evenNumsFibList中的所有内容,或者修改循环以仅添加列表中尚未包含的偶数。

假设你的缩进不正确。如果您的缩进实际上是您想要的,那么您只是在while循环中错过了一个右括号。

答案 3 :(得分:0)

我错过了什么吗?为什么需要创建列表?你只需要一个偶数值的数字?对?如果我理解正确,你可以用10行代码得到你的总和...我没有Java IDE opend,所以我会给你Pythone代码。如果它是您需要的,我会将其转换为Java。

def fib(n=4000001):    # write Fibonacci series up to n
    r = 0
    a, b = 0, 1
    while b < n:
        if not b%2 :
            print(b, end=' ')
            r += b
        a, b = b, a+b
    return r

OUTPUT:
2 8 34 144 610 2584 10946 46368 196418 832040 3524578 
sum = 4613732

答案 4 :(得分:0)

public class Euler002 {

int counter = 0;

public int getCounter () {
    return counter;
}

public int getFibTotal () {
    final int UPPER_LIMIT = 4000000; 
    int fib1 = 0;
    int fib2 = 1;
    int newFib = fib1 + fib2;
    int total = 0; 

    while (newFib < UPPER_LIMIT) {
        counter++;
        fib1 = fib2;
        fib2 = newFib;
        newFib = fib1 + fib2;
        if ((newFib % 2) == 0) {
            total += newFib;
        }
    }
    return total;
}

/**
 * @param args
 */
public static void main(String[] args) {
    Euler002 euler002 = new Euler002();
    int total = euler002.getFibTotal();
    System.out.println(" Counter = " + euler002.getCounter() + " And Fib Total is " + total);
}

}