Created
January 3, 2012 19:17
-
-
Save TimToady/1556435 to your computer and use it in GitHub Desktop.
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
constant @primes = 2, 3, -> $n is copy { repeat { $n += 2 } until $n %% none @primes ... * > sqrt $n; $n; } ... *; | |
multi factors(1) { 1 } | |
multi factors(Int $remainder is copy) { | |
gather for @primes -> $factor { | |
if $factor * $factor > $remainder { | |
take $remainder if $remainder > 1; | |
last; | |
} | |
# How many times can we divide by this prime? | |
while $remainder %% $factor { | |
take $factor; | |
last if ($remainder div= $factor) === 1; | |
} | |
} | |
} | |
say factors(536870911).perl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment