Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Build a map linking numbers to their dividors lists
- */
- def dividors(upto) {
- def returned = new TreeMap()
- for(root in 1G..upto) {
- def increment = 1G
- def value
- while((value = root*increment)<upto) {
- if(!returned.containsKey(value)) {
- returned.put(value, new TreeSet())
- returned[value] << 1G
- }
- if(value>root) returned[value].add(root)
- if(value>increment) returned[value].add(increment)
- increment++
- }
- }
- return returned
- }
- def dividorsSums(upto) {
- def divides = dividors(upto)
- // println divides
- def sums = new TreeMap()
- divides.each { k, v ->
- sums[k] = v.sum()
- }
- return sums
- }
- def amicable(upto) {
- def sums = dividorsSums(upto)
- // println sums
- def amicableList = []
- sums.each {k, v ->
- if(sums[v]==k && k!=v)
- amicableList << k
- }
- return amicableList
- }
- long start = System.currentTimeMillis();
- // def amicables = amicable(300G)
- def amicables = amicable(10000G)
- println amicables
- println amicables.sum()
- long end = System.currentTimeMillis();
- println "duration "+((end-start)/1000.0)+" s";
Add Comment
Please, Sign In to add comment