Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. /**
  2. * Build a map linking numbers to their dividors lists
  3. */
  4. def dividors(upto) {
  5. def returned = new TreeMap()
  6. for(root in 1G..upto) {
  7. def increment = 1G
  8. def value
  9. while((value = root*increment)<upto) {
  10. if(!returned.containsKey(value)) {
  11. returned.put(value, new TreeSet())
  12. returned[value] << 1G
  13. }
  14. if(value>root) returned[value].add(root)
  15. if(value>increment) returned[value].add(increment)
  16. increment++
  17. }
  18. }
  19. return returned
  20. }
  21.  
  22. def dividorsSums(upto) {
  23. def divides = dividors(upto)
  24. // println divides
  25. def sums = new TreeMap()
  26. divides.each { k, v ->
  27. sums[k] = v.sum()
  28. }
  29. return sums
  30. }
  31.  
  32. def amicable(upto) {
  33. def sums = dividorsSums(upto)
  34. // println sums
  35. def amicableList = []
  36. sums.each {k, v ->
  37. if(sums[v]==k && k!=v)
  38. amicableList << k
  39. }
  40. return amicableList
  41. }
  42.  
  43. long start = System.currentTimeMillis();
  44.  
  45. // def amicables = amicable(300G)
  46. def amicables = amicable(10000G)
  47. println amicables
  48. println amicables.sum()
  49.  
  50. long end = System.currentTimeMillis();
  51. println "duration "+((end-start)/1000.0)+" s";
Add Comment
Please, Sign In to add comment