aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-171/polettix/blog.txt1
-rw-r--r--challenge-171/polettix/blog1.txt1
-rw-r--r--challenge-171/polettix/perl/ch-1.pl33
-rw-r--r--challenge-171/polettix/perl/ch-2.pl13
-rw-r--r--challenge-171/polettix/raku/ch-1.raku28
-rw-r--r--challenge-171/polettix/raku/ch-2.raku7
6 files changed, 83 insertions, 0 deletions
diff --git a/challenge-171/polettix/blog.txt b/challenge-171/polettix/blog.txt
new file mode 100644
index 0000000000..2e8549d9fc
--- /dev/null
+++ b/challenge-171/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/06/29/pwc171-abundant-number/
diff --git a/challenge-171/polettix/blog1.txt b/challenge-171/polettix/blog1.txt
new file mode 100644
index 0000000000..79d431f270
--- /dev/null
+++ b/challenge-171/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/06/30/pwc171-first-class-function/
diff --git a/challenge-171/polettix/perl/ch-1.pl b/challenge-171/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..11c197ea5d
--- /dev/null
+++ b/challenge-171/polettix/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util 'sum';
+
+my $n = shift // 20;
+my @abundants;
+my $candidate = 945;
+while (@abundants < $n) {
+ push @abundants, $candidate if is_abundant($candidate);
+ $candidate += 2;
+}
+say join ', ', @abundants;
+
+sub is_abundant ($n) { $n < sum(proper_positive_divisors($n)) }
+
+sub proper_positive_divisors ($n) {
+ return unless $n;
+ $n = -$n if $n < 0;
+ my (@lows, @highs) = (1);
+ my ($lo, $hi) = (2, $n);
+ while ($lo < $hi) {
+ if ($n % $lo == 0) {
+ push @lows, $lo;
+ $hi = int($n / $lo);
+ unshift @highs, $hi if $hi != $lo;
+ }
+ ++$lo;
+ }
+ return (@lows, @highs);
+}
diff --git a/challenge-171/polettix/perl/ch-2.pl b/challenge-171/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..ffb302d27f
--- /dev/null
+++ b/challenge-171/polettix/perl/ch-2.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+my $h1 = compose(sub { $_[0] + 1 }, sub { $_[0] * 2 });
+say $h1->(1);
+
+my $h2 = compose(sub { $_[0] * 2 }, sub { $_[0] + 1 });
+say $h2->(1);
+
+sub compose ($f, $g) { sub { $f->($g->(@_)) } }
diff --git a/challenge-171/polettix/raku/ch-1.raku b/challenge-171/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..c5aae9be19
--- /dev/null
+++ b/challenge-171/polettix/raku/ch-1.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (Int:D $n where * > 0 = 20) {
+ my @abundants;
+ my $candidate = 945;
+ while @abundants < $n {
+ @abundants.push: $candidate if is-abundant($candidate);
+ $candidate += 2;
+ }
+ put @abundants.join(', ');
+}
+
+sub is-abundant (Int:D $n) { $n < [+] proper-positive-divisors($n) }
+
+sub proper-positive-divisors (Int:D $n is copy where * != 0) {
+ $n = $n.abs;
+ my (@lows, @highs) = 1,;
+ my ($lo, $hi) = (2, $n);
+ while $lo < $hi {
+ if $n %% $lo {
+ @lows.push: $lo;
+ $hi = ($n / $lo).Int;
+ @highs.unshift: $hi if $hi != $lo;
+ }
+ ++$lo;
+ }
+ return [@lows.Slip, @highs.Slip];
+}
diff --git a/challenge-171/polettix/raku/ch-2.raku b/challenge-171/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..51bbd953bb
--- /dev/null
+++ b/challenge-171/polettix/raku/ch-2.raku
@@ -0,0 +1,7 @@
+#!/usr/bin/env raku
+use v6;
+
+my $h1 = compose(-> ($x, $y) { $x / $y }, -> ($x, $y) { $y, $x });
+say $h1(2, 10);
+
+sub compose (&f, &g) { sub {f(g(@_))} }