Created
December 19, 2011 05:13
-
-
Save janoulle/1495500 to your computer and use it in GitHub Desktop.
Problem 2 - Project Euler in Java by Jane Ullah
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author: Jane Ullah | |
//Date: 12/19/2011 | |
//Site: janetalkscode.com | |
long a1 = 0L, a2 = 1L; | |
long nextTerm = 0L; | |
long sumEvenTerms = 0L; | |
boolean fourMillionthReached = false; | |
while ( !fourMillionthReached ) { | |
if (nextTerm >= 4000000) { | |
fourMillionthReached = true; | |
} | |
else { | |
nextTerm = a1 + a2; | |
if (nextTerm%2 == 0){ | |
sumEvenTerms += nextTerm; | |
} | |
a1 = a2; | |
a2 = nextTerm; | |
} | |
} | |
System.out.print("Even sum is: " + sumEvenTerms); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment