aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/athanasius
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-05-15 11:08:26 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-05-15 11:08:26 +0100
commitfc48282c8b8d04fa37f56ca0b624af0c2e46fcfe (patch)
tree6108cfccf7da4a669d7ee4d7404ddee5109f966e /challenge-008/athanasius
parent0100314ccc1a613f5e7cf8c0804ab7d7097cb5df (diff)
downloadperlweeklychallenge-club-fc48282c8b8d04fa37f56ca0b624af0c2e46fcfe.tar.gz
perlweeklychallenge-club-fc48282c8b8d04fa37f56ca0b624af0c2e46fcfe.tar.bz2
perlweeklychallenge-club-fc48282c8b8d04fa37f56ca0b624af0c2e46fcfe.zip
- Added solutions by Athanasius.
Diffstat (limited to 'challenge-008/athanasius')
-rw-r--r--challenge-008/athanasius/perl5/ch-1.pl49
-rw-r--r--challenge-008/athanasius/perl5/ch-2.pl39
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-008/athanasius/perl5/ch-1.pl b/challenge-008/athanasius/perl5/ch-1.pl
new file mode 100644
index 0000000000..0659e1414b
--- /dev/null
+++ b/challenge-008/athanasius/perl5/ch-1.pl
@@ -0,0 +1,49 @@
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $TARGET => 5;
+
+MAIN:
+{
+ my $count = 0;
+
+ # It is known that if any odd number n is perfect, n > 10^1500; so only even
+ # numbers need be considered. By the Euclid-Euler Theorem, an even number n
+ # is a perfect number if and only if n = 2^(k-1)*(2^k-1), where 2^k-1 is
+ # prime. So the perfect numbers are a subset of the positive integers n of
+ # the form n = 2^(k-1)*(2^k-1), where k is a positive integer.
+
+ for (my $k = 1; $count < $TARGET; ++$k)
+ {
+ my $n = (2 ** ($k - 1)) * (2 ** $k - 1);
+
+ if (is_perfect($n))
+ {
+ print "$n\n";
+ ++$count;
+ }
+ }
+}
+
+# A positive integer n is perfect if and only if n is equal to the sum of its
+# positive proper divisors (factors). (Equivalently, n is perfect if and only if
+# it is equal to half the sum of its divisors, where the latter include n
+# itself).
+
+sub is_perfect
+{
+ my ($n) = @_;
+
+ return 0 if $n == 1; # 1 is not a perfect number
+
+ my $max = int(sqrt($n) + 0.5);
+ my $sum = 1; # Every positive integer has 1 as a factor
+
+ for my $d (2 .. $max)
+ {
+ $sum += $d + ($n / $d) if ($n % $d) == 0;
+ }
+
+ return $n == $sum;
+}
diff --git a/challenge-008/athanasius/perl5/ch-2.pl b/challenge-008/athanasius/perl5/ch-2.pl
new file mode 100644
index 0000000000..89bd6dab3a
--- /dev/null
+++ b/challenge-008/athanasius/perl5/ch-2.pl
@@ -0,0 +1,39 @@
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $PREFIX => ' ';
+const my $SPACE => ' ';
+
+MAIN:
+{
+ my @input = ('This', 'is', 'a test of the', 'center function');
+ my @output = center(@input);
+
+ print "\n";
+ print $PREFIX . $_ . "\n" for @output;
+}
+
+sub center
+{
+ my @lines = @_;
+ my $max = 0;
+ my @lengths;
+
+ for my $line (@lines)
+ {
+ my $len = length $line;
+ push @lengths, $len;
+ $max = $len if $len > $max;
+ }
+
+ my @centered;
+
+ for my $i (0 .. $#lines)
+ {
+ my $spaces = int(($max - $lengths[$i]) / 2);
+ push @centered, ($SPACE x $spaces) . $lines[$i];
+ }
+
+ return @centered;
+}