Skip to content

Instantly share code, notes, and snippets.

@TimToady
Created January 3, 2012 19:17
Show Gist options
  • Save TimToady/1556435 to your computer and use it in GitHub Desktop.
Save TimToady/1556435 to your computer and use it in GitHub Desktop.
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