Created
February 8, 2011 15:57
-
-
Save Riduidel/816638 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(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) { | |
| if(factor<number) | |
| returned << factor | |
| def quotient = (number/factor).longValue() | |
| if(quotient<number) | |
| returned << quotient | |
| } | |
| } | |
| } | |
| return returned | |
| } | |
| def getAbundantsBelow(max) { | |
| def returned = new TreeSet() | |
| def dividors | |
| def dividorsSum | |
| for(number in 1L..max) { | |
| dividors = dividorsOf(number) | |
| dividorsSum = dividors.inject(0) { result, d -> result+d } | |
| // println "for $number, dividors are $dividors and their sum is $dividorsSum" | |
| if(dividorsSum>number) { | |
| returned << number | |
| } | |
| } | |
| return returned | |
| } | |
| def getNonSumsBelow(TreeSet abundants, max) { | |
| def returned = new TreeSet() | |
| for(number in 1L..max) { | |
| // small optimization to narrow search space | |
| usableOnes = abundants.headSet(number) | |
| iterator = usableOnes.iterator() | |
| def found = false | |
| // Good old Java loop allowing anything | |
| while(iterator.hasNext() && !found) { | |
| found = usableOnes.contains(number-iterator.next()) | |
| } | |
| if(!found) | |
| returned << number | |
| } | |
| return returned | |
| } | |
| def MAX = 28123 | |
| long start = System.currentTimeMillis() | |
| def abundants = getAbundantsBelow(MAX) | |
| println "all abundants have been found, now searching non sums" | |
| // now, find numbers that are not sums of abundant numbers | |
| def nonSumOfAbundants = getNonSumsBelow(abundants, MAX) | |
| println nonSumOfAbundants.inject(0L) {result, value -> result+value } | |
| 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