开发者

Project Euler, problem 2- Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 8 years ago.

Improve this question

I'm a relatively new java programmer and I've been tinkering around with this program for the better part of the day now and I'm still stuck; I was hoping that you could help me with this.

So the program is supposed to meet the following requirements:

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.

This is my code:

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

...and this is the output that I'm getting:

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 l开发者_运维百科ist: 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

Obviously my while loop is contributing to my problems, but I don't know how to fix it.

Would greatly appreciate your help,

Haque


If you take a closer look at the numbers in the Fibonacci sequence that you actually need (only the even ones need to be summed), you will see a pattern:

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

Notice that every 3rd number starting after 0 is even. Therefore, you can eliminate any checking for evenness if you calculate every third Fibonacci number. Looking again at the sequence, you can see that if k is the present even Fibonacci number you are looking at, and j is the one previous, the next even Fibonacci number n can be obtained by:

n = 4k + j

So in Java, you could try something like this:

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


Looks like you are missing the close brackets on the while loop. So the other for's are running within it.

So:

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++)
    {...


Am I missing something? Why do you need to create list? You just need a sum of even-valued numbers? Right? If I understand you correctly you can get your sum in 10 lines of code... I don't have Java IDE opend, so I'll give you Pythone code. If it is what you need I'll convert it into 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


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

}


The problem is here:

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

You're adppending a whole new list of all the even numbers to the end of the list you already have.

You need to delete everything in evenNumsFibList before calling this loop again, or modify the loop to only add even numbers that are not already in the list.

That's assuming your indentation is incorrect. If your indentation is actually how you want it, then you're simply missing a closing bracket on your while loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜