aboutsummaryrefslogtreecommitdiff
path: root/challenge-008
diff options
context:
space:
mode:
authordrclaw1394 <drclaw@mac.com>2019-06-04 18:58:04 +1000
committerGitHub <noreply@github.com>2019-06-04 18:58:04 +1000
commitb773b5dadb3d56e1f35390f8198530411809b237 (patch)
treeb76350f0db97f1c37cd6c9c756663285989b7ce5 /challenge-008
parent371ce98e5f90748acb5ffaf9a0df5ae72454c571 (diff)
parentfb72656c0b4dac4fae14404bc9114c65081271a0 (diff)
downloadperlweeklychallenge-club-b773b5dadb3d56e1f35390f8198530411809b237.tar.gz
perlweeklychallenge-club-b773b5dadb3d56e1f35390f8198530411809b237.tar.bz2
perlweeklychallenge-club-b773b5dadb3d56e1f35390f8198530411809b237.zip
Merge pull request #10 from manwar/master
Update to W11
Diffstat (limited to 'challenge-008')
-rw-r--r--challenge-008/e-choroba/blog.txt1
-rw-r--r--challenge-008/e-choroba/perl5/ch-1.pl15
-rw-r--r--challenge-008/e-choroba/perl5/ch-2.pl21
-rw-r--r--challenge-008/e-choroba/perl5/ch-2a.pl67
-rw-r--r--challenge-008/e-choroba/perl5/ch-2b.pl49
5 files changed, 153 insertions, 0 deletions
diff --git a/challenge-008/e-choroba/blog.txt b/challenge-008/e-choroba/blog.txt
new file mode 100644
index 0000000000..6b621dfadd
--- /dev/null
+++ b/challenge-008/e-choroba/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/e_choroba/2019/05/perl-weekly-challenge-008-perfect-numbers-and-centring.html
diff --git a/challenge-008/e-choroba/perl5/ch-1.pl b/challenge-008/e-choroba/perl5/ch-1.pl
new file mode 100644
index 0000000000..28beb074e1
--- /dev/null
+++ b/challenge-008/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+use Path::Tiny;
+use List::Util qw{ max };
+
+sub center {
+ my @lines = @_;
+ my $max_length = max(map length, @lines);
+ return map +(' ' x (($max_length - length) / 2)) . $_, @lines
+}
+
+my @lines = path(shift)->lines;
+print for center(@lines);
diff --git a/challenge-008/e-choroba/perl5/ch-2.pl b/challenge-008/e-choroba/perl5/ch-2.pl
new file mode 100644
index 0000000000..a110fbcd68
--- /dev/null
+++ b/challenge-008/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+use List::Util qw{ sum };
+
+use constant COUNT => 5;
+
+sub is_perfect {
+ my ($n) = @_;
+ my @divisors = grep 0 == $n % $_, 1 .. $n - 1;
+ return sum(@divisors) == $n
+}
+
+my $n = 2;
+my $so_far = 0;
+while ($so_far < COUNT) {
+ ++$so_far, say $n if is_perfect($n);
+ ++$n
+}
diff --git a/challenge-008/e-choroba/perl5/ch-2a.pl b/challenge-008/e-choroba/perl5/ch-2a.pl
new file mode 100644
index 0000000000..f29fc0eb56
--- /dev/null
+++ b/challenge-008/e-choroba/perl5/ch-2a.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+use integer;
+
+use constant COUNT => 5;
+
+my @primes = (2);
+
+sub sum_subsets {
+ my ($p, @numbers) = @_;
+ my $binary = 1;
+ my $sum = 1; # Always a divisor.
+ my %seen;
+
+ while (1) {
+ my @zero_ones = split //, sprintf '%b', $binary++;
+ unshift @zero_ones, (0) x (@numbers - @zero_ones);
+ last unless grep ! $_, @zero_ones;
+
+ my $divisor = 1;
+ $divisor *= $numbers[$_] for grep $zero_ones[$_], 0 .. $#zero_ones;
+ next if exists $seen{$divisor};
+
+ undef $seen{$divisor};
+ $sum += $divisor;
+ last if $sum > $p;
+ }
+ return $sum
+}
+
+
+sub factorize {
+ my ($n) = @_;
+ my $prime_index = 0;
+ my ($f, @factors);
+
+ while ($n > 1) {
+ $f = $primes[$prime_index] // ($f + 1);
+
+ if ($n % $f) {
+ $prime_index++;
+
+ } else {
+ push @factors, $f;
+ $n /= $f;
+ }
+ }
+ return @factors
+}
+
+my $perfect_tally = 0;
+my $p = 2;
+while ($perfect_tally < COUNT) {
+ my @factors = factorize($p);
+ if (1 == @factors and $p > $primes[-1]) {
+ push @primes, $p;
+ }
+ my $product = sum_subsets($p, @factors);
+ if ($product == $p) {
+ $perfect_tally++;
+ say $p;
+ }
+} continue {
+ $p++;
+}
diff --git a/challenge-008/e-choroba/perl5/ch-2b.pl b/challenge-008/e-choroba/perl5/ch-2b.pl
new file mode 100644
index 0000000000..a726e436f2
--- /dev/null
+++ b/challenge-008/e-choroba/perl5/ch-2b.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+use constant COUNT => 5;
+
+my @primes = (2, 3);
+
+
+sub add_primes {
+ my ($upto) = @_;
+
+ PRIME:
+ for my $n ($primes[-1] + 2 .. $upto) {
+ for my $p (@primes) {
+ next PRIME if 0 == $n % $p;
+
+ last if $p > sqrt $n;
+ }
+ push @primes, $n;
+ }
+}
+
+
+sub is_prime {
+ my ($n) = @_;
+ return if $n == 1;
+
+ add_primes($n) if $n > $primes[-1];
+
+ for my $p (@primes) {
+ return 1 if $p >= $n;
+
+ return if 0 == $n % $p;
+ }
+}
+
+my $n = 1;
+my $tally = 0;
+while ($tally < COUNT) {
+ if (is_prime($n) && is_prime(2 ** $n - 1)) {
+ say +(2 ** $n - 1) * 2 ** ($n - 1);
+ ++$tally;
+ }
+ ++$n;
+}
+
+