Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[euler/prob003] simplify algorithm

composite numbers won't divide the reduced $n as their factors
have already been seen and divided out
  • Loading branch information...
commit 294bc616f21bcd1b1c3d1a631727582d87d641a2 1 parent 1f2506c
@gerdr gerdr authored
Showing with 2 additions and 18 deletions.
  1. +0 −13 euler/prob003-gerdr-cheating.pl
  2. +2 −5 euler/prob003-gerdr.pl
View
13 euler/prob003-gerdr-cheating.pl
@@ -1,13 +0,0 @@
-use v6;
-
-sub largest-prime-factor($n is copy) {
- for 2, 3, *+2 ... * {
- next unless $n %% $_ and .is-prime;
- repeat while $n %% $_ {
- $n div= $_
- }
- return $_ if $_ > $n;
- }
-}
-
-say largest-prime-factor(600_851_475_143);
View
7 euler/prob003-gerdr.pl
@@ -1,14 +1,11 @@
use v6;
sub largest-prime-factor($n is copy) {
- my @primes;
for 2, 3, *+2 ... * {
- next if $_ %% any @primes.grep(->$p { $p * $p <= $_ or last });
- @primes.push($_);
while $n %% $_ {
- $n div= $_
+ $n div= $_;
+ return $_ if $_ > $n;
}
- return $_ if $_ > $n;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.