Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package euler_02;
  2.  
  3. /**
  4.  *
  5.  * @author Steve
  6.  *
  7.  *  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
  8. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  9. By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  10.  */
  11. public class Euler_02 {
  12.  
  13.     /**
  14.      * @param args the command line arguments
  15.      */
  16.     private static final int MAX_TERM_VALUE = 4000000;
  17.     //using a constant value instead of while <4000000 etc.
  18.  
  19.     public static void main(String[] args) {
  20.         //%2 == 0 for even terms  
  21.         //2 var's for the values,
  22.         //A temp var for the addition & sum for running tot
  23.         int fibA = 0;
  24.         int fibB = 1;
  25.         int fibTemp = 0;
  26.         int sum = 0;
  27.  
  28.         while (true) {
  29.             //adds the two previous terms
  30.             fibTemp = fibA + fibB;
  31.             //replaces the previous terms
  32.             fibA = fibB;
  33.             fibB = fibTemp;
  34.             //uses constant to govern the while...        
  35.             if (fibTemp >= MAX_TERM_VALUE) {
  36.                 break;
  37.             }//end if
  38.  
  39.             //check next number to see if even, if yes then add to running total.
  40.             if (fibTemp % 2 == 0) {
  41.                 sum = sum + fibTemp;
  42.             }//endif
  43.  
  44.         }//end while
  45.  
  46.  
  47.         System.out.printf("How do I turn this on???" + fibA + "\n" + fibB + "\n" + fibTemp + "\n" + sum);
  48.     }
  49. }
Add Comment
Please, Sign In to add comment