diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2020-12-23 21:43:57 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2020-12-23 21:43:57 +0000 |
| commit | da493d8ab70e31de90b2d24c8ccd49f65aec535d (patch) | |
| tree | b5bfcd8aefca733432991db2d975628fc801e685 | |
| parent | 3414c0d0389b7c0a9c4a222f0aa1080a5906dce3 (diff) | |
| download | perlweeklychallenge-club-da493d8ab70e31de90b2d24c8ccd49f65aec535d.tar.gz perlweeklychallenge-club-da493d8ab70e31de90b2d24c8ccd49f65aec535d.tar.bz2 perlweeklychallenge-club-da493d8ab70e31de90b2d24c8ccd49f65aec535d.zip | |
Remove need to call sqrt()
| -rw-r--r-- | challenge-085/paulo-custodio/perl/ch-2.pl | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/challenge-085/paulo-custodio/perl/ch-2.pl b/challenge-085/paulo-custodio/perl/ch-2.pl index a0fd2373bf..043628f460 100644 --- a/challenge-085/paulo-custodio/perl/ch-2.pl +++ b/challenge-085/paulo-custodio/perl/ch-2.pl @@ -32,8 +32,7 @@ say is_perfect_power($n); sub is_perfect_power { my($n) = @_; - my $max_factor = sqrt($n)+1; - find_primes($max_factor); # fill list of prime numbers up to sqrt(n)+1 + find_primes($n); # fill list of prime numbers up to sqrt(n) for my $prime (@primes) { my $exp = 1; my $power; @@ -48,15 +47,15 @@ sub is_perfect_power { } sub find_primes { - my($max) = @_; + my($n) = @_; my $prime = 2; do { push @primes, $prime; - for (my $f = $prime*2; $f < $max; $f += $prime) { + for (my $f = $prime*2; $f*$f <= $n; $f += $prime) { $sieve[$f] = 1; } do { $prime++ - } while ($prime < $max && $sieve[$prime]); - } while ($prime < $max); + } while ($prime*$prime < $n && $sieve[$prime]); + } while ($prime*$prime < $n); } |
