Created
February 16, 2011 09:39
-
-
Save Riduidel/829095 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
| import groovyx.gpars.GParsPool | |
| /** | |
| * Extract decimals of n in correct order | |
| */ | |
| def decimals(int n) { | |
| def returned = [] | |
| while(n>0) { | |
| returned << n%10 | |
| n/=10 | |
| } | |
| return returned.reverse() | |
| } | |
| /** | |
| * Check if number is made of sum of power of fifths from its decimal numbers | |
| */ | |
| boolean isMadeofFifths(def n, def fifths) { | |
| return n>1 && n==decimals(n).inject(0, { result, i -> result+fifths[i] }) | |
| } | |
| @Grab(group="org.codehaus.gpars", module="gpars", version="0.11") | |
| def getRealSumsOfFifths(fifths) { | |
| GParsPool.withPool { | |
| // Upper bound not obvious to choose | |
| def testedNumbers = 1..1000000 | |
| return testedNumbers.findAllParallel { n -> | |
| return isMadeofFifths(n, fifths) | |
| } | |
| } | |
| } | |
| long start = System.currentTimeMillis(); | |
| /* presence of 0 is a hack to ensure items are correctly stored) */ | |
| fifths = (0..9).collect { it.power(5) } | |
| /* Now come GPars time ! */ | |
| builtOfFifth = getRealSumsOfFifths(fifths) | |
| println builtOfFifth.sum() | |
| 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