Note that there are some explanatory texts on larger screens.

plurals
  1. POProject Euler, problem 2- Java
    primarykey
    data
    text
    <p>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. </p> <p>So the program is supposed to meet the following requirements:</p> <blockquote> <p>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:</p> <p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</p> <p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p> </blockquote> <p>This is my code:</p> <pre><code> //Generates Fibonacci sequence while (fibNum &lt; 144) { int lastValue = (Integer) fibList.get(fibList.size()-1); int secondToLastValue = (Integer) fibList.get(fibList.size()-2); fibNum = secondToLastValue + lastValue; if (fibNum &lt; 144) { fibList.add(fibNum); } //Picks out the even numbers from limitFibList for (int i = 0; i &lt; 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 &lt; evenNumsFibList.size(); i++) { sum += (Integer) evenNumsFibList.get(i); } </code></pre> <p>...and this is the output that I'm getting:</p> <pre><code>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 </code></pre> <p>Obviously my while loop is contributing to my problems, but I don't know how to fix it. </p> <p>Would greatly appreciate your help,</p> <p>Haque</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload