diff options
author | Guillermo Ramos | 2019-05-19 16:43:56 +0200 |
---|---|---|
committer | Guillermo Ramos | 2019-05-19 16:51:42 +0200 |
commit | 1cb63654f2c308f40b500b3f66fb3c1286aaa428 (patch) | |
tree | 4b86030e3ae76f711a41297786067ad811c49be7 /008/ch1.pl | |
parent | 76826f342c29635483cb6b2e6faded58d5fffbd4 (diff) | |
download | perlweekly-1cb63654f2c308f40b500b3f66fb3c1286aaa428.tar.gz |
[008#1] Split into two versions
Diffstat (limited to '008/ch1.pl')
-rwxr-xr-x | 008/ch1.pl | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -3,8 +3,26 @@ use strict; use warnings; -my @mersennes = (2, 3, 5, 7, 13); +my @primes; -foreach my $p (@mersennes) { - print 2**($p - 1) * (2**$p - 1), "\n"; +sub perfect { + my $sum = 0; + my $n = shift; + my $candidate = 1; + while ($candidate*2 <= $n) { + $sum += $candidate if $n % $candidate == 0; + $candidate++; + } + + return $n == $sum; +} + +my $n = 1; +my $perfects = 0; +while ($perfects < 5) { + if (perfect($n)) { + print $n, "\n"; + $perfects++; + } + $n++; } |