Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package euler_02;
- /**
- *
- * @author Steve
- *
- * 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.
- */
- public class Euler_02 {
- /**
- * @param args the command line arguments
- */
- private static final int MAX_TERM_VALUE = 4000000;
- //using a constant value instead of while <4000000 etc.
- public static void main(String[] args) {
- //%2 == 0 for even terms
- //2 var's for the values,
- //A temp var for the addition & sum for running tot
- int fibA = 0;
- int fibB = 1;
- int fibTemp = 0;
- int sum = 0;
- while (true) {
- //adds the two previous terms
- fibTemp = fibA + fibB;
- //replaces the previous terms
- fibA = fibB;
- fibB = fibTemp;
- //uses constant to govern the while...
- if (fibTemp >= MAX_TERM_VALUE) {
- break;
- }//end if
- //check next number to see if even, if yes then add to running total.
- if (fibTemp % 2 == 0) {
- sum = sum + fibTemp;
- }//endif
- }//end while
- System.out.printf("How do I turn this on???" + fibA + "\n" + fibB + "\n" + fibTemp + "\n" + sum);
- }
- }
Add Comment
Please, Sign In to add comment