Created
March 26, 2012 04:36
-
-
Save ccw/2202955 to your computer and use it in GitHub Desktop.
[Project Euler in Groovy] - P10
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
def primes = [2, 3, 5, 7, 11, 13, 17, 19] | |
def MAX = 2000000 | |
double sum = primes.sum() | |
def i = primes.get(primes.size() - 1) + 2 | |
while (MAX > i) { | |
def found = true | |
for (int x = 0; x < primes.size() && primes.get(x)*primes.get(x) <= i; x++) { | |
if( i % primes.get(x) == 0) { | |
found = false | |
break | |
} | |
} | |
if(found) { | |
primes << i | |
sum += i | |
} | |
i+=2 | |
while(i % (Math.sqrt(i)) == 0) { | |
println "skip $i" | |
i+=2 | |
} | |
} | |
println "sum -> ${new java.text.DecimalFormat('###########0').format(sum)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment