Skip to content

Instantly share code, notes, and snippets.

@ccw
Created March 26, 2012 04:36
Show Gist options
  • Save ccw/2202955 to your computer and use it in GitHub Desktop.
Save ccw/2202955 to your computer and use it in GitHub Desktop.
[Project Euler in Groovy] - P10
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