summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2019-05-19 16:43:56 +0200
committerGuillermo Ramos2019-05-19 16:51:42 +0200
commit1cb63654f2c308f40b500b3f66fb3c1286aaa428 (patch)
tree4b86030e3ae76f711a41297786067ad811c49be7
parent76826f342c29635483cb6b2e6faded58d5fffbd4 (diff)
downloadperlweekly-1cb63654f2c308f40b500b3f66fb3c1286aaa428.tar.gz
[008#1] Split into two versions
-rwxr-xr-x008/ch1.pl24
-rwxr-xr-x008/ch1_v2.pl10
2 files changed, 31 insertions, 3 deletions
diff --git a/008/ch1.pl b/008/ch1.pl
index 0327467..81d590c 100755
--- a/008/ch1.pl
+++ b/008/ch1.pl
@@ -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++;
}
diff --git a/008/ch1_v2.pl b/008/ch1_v2.pl
new file mode 100755
index 0000000..d5c807e
--- /dev/null
+++ b/008/ch1_v2.pl
@@ -0,0 +1,10 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+my @mersenne_exps = (2, 3, 5, 7, 13);
+
+foreach my $exp (@mersenne_exps) {
+ print 2**($exp - 1) * (2**$exp - 1), "\n";
+}