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 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