Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created February 8, 2011 14:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Riduidel/816494 to your computer and use it in GitHub Desktop.
def dividorsOf(existingDividers, number) {
def returned = new TreeSet()
def max = Math.sqrt(number).longValue()+1L
def range = max..1L
for(factor in range) {
if(!returned.contains(factor)) {
if(number%factor==0) {
returned.add(factor)
returned.add((number/factor).longValue())
/* // Now check already existing dividers. if factor is in it, use this knowledge wisely
if(existingDividers.containsKey(factor)) {
// we directly add all already existing dividers of factor with their counterpart
existingDividers[factor].each { subfactor ->
returned.add(subfactor)
returned.add((number/subfactor).longValue())
}
}
*/ }
}
}
return returned
}
long start = System.currentTimeMillis();
// base of the triangle number
def size = 1L
// current triangle number value
def currentSum = 0L
def dividors = []
def dividorsCount = 0
def existingDividers = new TreeMap()
while(dividorsCount<500L) {
currentSum += size
dividors = dividorsOf(existingDividers, currentSum)
dividorsCount = dividors.size()
// existingDividers[currentSum]=dividors
if(size%1000==0) {
println size +" => "+currentSum +" => "/*+dividors*/+" ($dividorsCount)"
}
size++
}
println currentSum+" => "+dividors
long end = System.currentTimeMillis();
println "duration "+((end-start)/1000.0)+" s";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment