aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-109/dave-jacoby/blog.txt1
-rw-r--r--challenge-109/dave-jacoby/node/ch-1.js17
-rw-r--r--challenge-109/dave-jacoby/perl/ch-1.pl35
-rw-r--r--challenge-109/dave-jacoby/perl/ch-2.pl79
4 files changed, 132 insertions, 0 deletions
diff --git a/challenge-109/dave-jacoby/blog.txt b/challenge-109/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..98a826c8ac
--- /dev/null
+++ b/challenge-109/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/04/21/iterations-and-permutations-perl-weekly-challenge-109.html \ No newline at end of file
diff --git a/challenge-109/dave-jacoby/node/ch-1.js b/challenge-109/dave-jacoby/node/ch-1.js
new file mode 100644
index 0000000000..72452fdcb5
--- /dev/null
+++ b/challenge-109/dave-jacoby/node/ch-1.js
@@ -0,0 +1,17 @@
+"use strict";
+
+let list = Array(20)
+ .fill()
+ .map((x, i) => i + 1)
+ .map((x) => chowla(x));
+console.log(list.join(", "));
+
+function chowla(n) {
+ return Array(n)
+ .fill()
+ .map((x, i) => i + 1)
+ .filter((x) => x != 1)
+ .filter((x) => x != n)
+ .filter((x) => n % x == 0)
+ .reduce((a, v) => a + v, 0);
+}
diff --git a/challenge-109/dave-jacoby/perl/ch-1.pl b/challenge-109/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..96de494c1a
--- /dev/null
+++ b/challenge-109/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use List::Util qw{sum0};
+
+my @test = ( 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 );
+
+say join ', ', @test;
+say join ', ', map { chowla($_) } 1 .. 20;
+say join ', ', map { chowla2($_) } 1 .. 20;
+
+# the old-fashioned for-loop way
+sub chowla ( $n ) {
+ my $c = 0;
+ for my $i ( 1 .. $n ) {
+ my $m = $n % $i;
+ next if $i == 1 || $i == $n;
+ $c += $i if $m == 0;
+ }
+ return $c;
+}
+
+# the new, hot functional approach
+# using sum0 because if given an empty list,
+# sum0 returns zero instead of undef
+sub chowla2 ( $n ) {
+ return sum0
+ grep { $n % $_ == 0 }
+ grep { $_ != 1 }
+ grep { $_ != $n } 1 .. $n;
+}
diff --git a/challenge-109/dave-jacoby/perl/ch-2.pl b/challenge-109/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..d1dba3614c
--- /dev/null
+++ b/challenge-109/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use Algorithm::Permute;
+
+my $map = <<'END';
+ (1) (3)
+ ╔══════════════╗ ╔══════════════╗
+ ║ ║ ║ ║
+ ║ a ║ ║ e ║
+ ║ ║ (2) ║ ║ (4)
+ ║ ┌───╫──────╫───┐ ┌───╫─────────┐
+ ║ │ ║ ║ │ │ ║ │
+ ║ │ b ║ ║ d │ │ f ║ │
+ ║ │ ║ ║ │ │ ║ │
+ ║ │ ║ ║ │ │ ║ │
+ ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │
+ │ c │ │ g │
+ │ │ │ │
+ │ │ │ │
+ └──────────────┘ └─────────────┘
+END
+
+four_squares( 1 .. 7 );
+
+sub four_squares ( @array ) {
+ my $array = join ', ', @array;
+ my $ap = Algorithm::Permute->new( \@array );
+ while ( my @perm = $ap->next ) {
+ my $b1 = _box_1(@perm);
+ my $b2 = _box_2(@perm);
+ next if $b1 != $b2;
+ my $b3 = _box_3(@perm);
+ next if $b1 != $b3;
+ my $b4 = _box_4(@perm);
+ next if $b1 != $b4;
+
+ my $a = $perm[0];
+ my $b = $perm[1];
+ my $c = $perm[2];
+ my $d = $perm[3];
+ my $e = $perm[4];
+ my $f = $perm[5];
+ my $g = $perm[6];
+
+ say <<"END";
+ a = $a e = $e
+ b = $b f = $f
+ c = $c g = $g
+ d = $d
+ Box1 = a + b = $a + $b = $b1
+ Box2 = b + c + d = $b + $c + $d = $b2
+ Box3 = d + e + f = $d + $e + $f = $b3
+ Box4 = f + g = $f + $g = $b4
+END
+
+ }
+
+}
+
+sub _box_1( @array ) {
+ return $array[0] + $array[1];
+}
+
+sub _box_2( @array ) {
+ return $array[1] + $array[2] + $array[3];
+}
+
+sub _box_3( @array ) {
+ return $array[3] + $array[4] + $array[5];
+}
+
+sub _box_4( @array ) {
+ return $array[5] + $array[6];
+}