Skip to content

Instantly share code, notes, and snippets.

@ccw
Created March 26, 2012 04:31
Show Gist options
  • Save ccw/2202938 to your computer and use it in GitHub Desktop.
Save ccw/2202938 to your computer and use it in GitHub Desktop.
[Project Euler in Groovy] - P5
def MIN = 2, MAX = 20
def primes = [2, 3, 5, 7, 11, 13, 17, 19]
def factors = []
while (MIN <= MAX) {
if (MIN in primes) {
factors << MIN
} else {
// find MIN factors
def fn = []
def x = MIN
while(!(x in primes)) {
def f = primes.find { x % it == 0 }
fn << f
x = (int) (x / f)
}
fn << x
// add factors
def i = 0
fn.each {
def found = false
for (int j = i; j < factors.size(); j++) {
if (it == factors.get(j)) {
i = j + 1
found = true
break
}
}
if (!found) {
factors << it
factors.sort()
println "factors changed => $factors"
}
}
}
MIN++
}
println "factors => ${factors}"
def sum = 1
factors.each { sum *= it }
println "result => ${sum}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment