aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarnesom <arne@bbop.org>2020-12-04 20:50:00 +0100
committerarnesom <arne@bbop.org>2020-12-04 20:50:00 +0100
commit04224b0f0d81675b8659b0d0bdc924e87e09ce0d (patch)
tree65dd0231677861a9774c9bad8dee16a7f4b04b3e
parentd19b0f983bbefca06f6139624711c079ac18eb6e (diff)
downloadperlweeklychallenge-club-04224b0f0d81675b8659b0d0bdc924e87e09ce0d.tar.gz
perlweeklychallenge-club-04224b0f0d81675b8659b0d0bdc924e87e09ce0d.tar.bz2
perlweeklychallenge-club-04224b0f0d81675b8659b0d0bdc924e87e09ce0d.zip
Arne Sommer 089
-rw-r--r--challenge-089/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-089/arne-sommer/perl/ch-1.pl54
-rwxr-xr-xchallenge-089/arne-sommer/perl/ch-2.pl37
-rwxr-xr-xchallenge-089/arne-sommer/perl/gcd-sum-perl54
-rwxr-xr-xchallenge-089/arne-sommer/perl/gcd-sum-perl-errorfix63
-rwxr-xr-xchallenge-089/arne-sommer/perl/magical-matrix-perl37
-rwxr-xr-xchallenge-089/arne-sommer/raku/ch-1.raku32
-rwxr-xr-xchallenge-089/arne-sommer/raku/ch-2.raku25
-rwxr-xr-xchallenge-089/arne-sommer/raku/gcd-sum32
-rwxr-xr-xchallenge-089/arne-sommer/raku/magical-matrix25
10 files changed, 360 insertions, 0 deletions
diff --git a/challenge-089/arne-sommer/blog.txt b/challenge-089/arne-sommer/blog.txt
new file mode 100644
index 0000000000..e1169933c5
--- /dev/null
+++ b/challenge-089/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/magical-sum.html
diff --git a/challenge-089/arne-sommer/perl/ch-1.pl b/challenge-089/arne-sommer/perl/ch-1.pl
new file mode 100755
index 0000000000..f831374a8b
--- /dev/null
+++ b/challenge-089/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#! /usr/bin/env perl
+
+use strict;
+use feature 'say';
+use feature 'signatures';
+no warnings qw(experimental::signatures);
+
+use Algorithm::Combinatorics 'combinations';
+use List::MoreUtils 'duplicates';
+use Getopt::Long;
+
+my $verbose = 0;
+GetOptions("verbose" => \$verbose);
+
+my $N = $ARGV[0] // die "Please specify an integer > 0";
+
+die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/;
+
+my @all = (1 .. $N);
+
+my $sum = 0;
+
+for my $pair (combinations(\@all, 2))
+{
+ $sum += gcd(@$pair);
+}
+
+say $sum;
+
+sub gcd ($a, $b)
+{
+ my @a = divisors($a);
+ my @b = divisors($b);
+ my @common = duplicates(@a, @b);
+ my $gcd = $common[$#common];
+
+ say ":: GCD($a, $b) -> $gcd" if $verbose;
+
+ return $gcd;
+}
+
+sub divisors ($number)
+{
+ my @divisors = (1);
+
+ for my $candidate (2 .. $number/2)
+ {
+ push(@divisors, $candidate) if $number % $candidate == 0;
+ }
+
+ push(@divisors, $number);
+
+ return @divisors;
+}
diff --git a/challenge-089/arne-sommer/perl/ch-2.pl b/challenge-089/arne-sommer/perl/ch-2.pl
new file mode 100755
index 0000000000..a1e6e58ae7
--- /dev/null
+++ b/challenge-089/arne-sommer/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#! /usr/bin/env perl
+
+use strict;
+use feature 'say';
+use Getopt::Long;
+use Algorithm::Combinatorics 'permutations';
+
+my $verbose = 0;
+my $all = 0;
+
+GetOptions("verbose" => \$verbose,
+ "all" => \$all);
+
+my @source = 1..9;
+
+for my $list (permutations(\@source))
+{
+ my @candidate = @$list;
+ say ":: " . join(@candidate, ", ") if $verbose;
+ my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate;
+
+ next unless $a + $b + $c == 15;
+ next unless $d + $e + $f == 15;
+ next unless $g + $h + $i == 15;
+ next unless $a + $d + $g == 15;
+ next unless $b + $e + $h == 15;
+ next unless $c + $f + $i == 15;
+ next unless $a + $e + $i == 15;
+ next unless $c + $e + $g == 15;
+
+ say "[ " . join(" ", @candidate[0..2]) . " ]";
+ say "[ " . join(" ", @candidate[3..5]) . " ]";
+ say "[ " . join(" ", @candidate[6..8]) . " ]";
+ say "" if $all;
+
+ last unless $all;
+}
diff --git a/challenge-089/arne-sommer/perl/gcd-sum-perl b/challenge-089/arne-sommer/perl/gcd-sum-perl
new file mode 100755
index 0000000000..f831374a8b
--- /dev/null
+++ b/challenge-089/arne-sommer/perl/gcd-sum-perl
@@ -0,0 +1,54 @@
+#! /usr/bin/env perl
+
+use strict;
+use feature 'say';
+use feature 'signatures';
+no warnings qw(experimental::signatures);
+
+use Algorithm::Combinatorics 'combinations';
+use List::MoreUtils 'duplicates';
+use Getopt::Long;
+
+my $verbose = 0;
+GetOptions("verbose" => \$verbose);
+
+my $N = $ARGV[0] // die "Please specify an integer > 0";
+
+die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/;
+
+my @all = (1 .. $N);
+
+my $sum = 0;
+
+for my $pair (combinations(\@all, 2))
+{
+ $sum += gcd(@$pair);
+}
+
+say $sum;
+
+sub gcd ($a, $b)
+{
+ my @a = divisors($a);
+ my @b = divisors($b);
+ my @common = duplicates(@a, @b);
+ my $gcd = $common[$#common];
+
+ say ":: GCD($a, $b) -> $gcd" if $verbose;
+
+ return $gcd;
+}
+
+sub divisors ($number)
+{
+ my @divisors = (1);
+
+ for my $candidate (2 .. $number/2)
+ {
+ push(@divisors, $candidate) if $number % $candidate == 0;
+ }
+
+ push(@divisors, $number);
+
+ return @divisors;
+}
diff --git a/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix b/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix
new file mode 100755
index 0000000000..5319d133ba
--- /dev/null
+++ b/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix
@@ -0,0 +1,63 @@
+#! /usr/bin/env perl
+
+use strict;
+use feature 'say';
+use feature 'signatures';
+no warnings qw(experimental::signatures);
+
+use Algorithm::Combinatorics 'combinations';
+# use List::MoreUtils 'duplicates';
+use Getopt::Long;
+
+my $verbose = 0;
+GetOptions("verbose" => \$verbose);
+
+my $N = $ARGV[0] // die "Please specify an integer > 0";
+
+die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/;
+
+my @all = (1 .. $N);
+
+my $sum = 0;
+
+for my $pair (combinations(\@all, 2))
+{
+ $sum += gcd(@$pair);
+}
+
+say $sum;
+
+sub gcd ($a, $b)
+{
+ my @a = divisors($a);
+ my @b = divisors($b);
+ my @common = duplicates(@a, @b);
+ my $gcd = $common[$#common];
+
+ say ":: GCD($a, $b) -> $gcd" if $verbose;
+
+ return $gcd;
+}
+
+sub divisors ($number)
+{
+ my @divisors = (1);
+
+ for my $candidate (2 .. $number/2)
+ {
+ push(@divisors, $candidate) if $number % $candidate == 0;
+ }
+
+ push(@divisors, $number);
+
+ return @divisors;
+}
+
+sub duplicates (@)
+{
+ my %seen = ();
+ my $k;
+ my $seen_undef;
+ return grep { 1 < (defined $_ ? $seen{$k = $_} : $seen_undef) }
+ grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_;
+}
diff --git a/challenge-089/arne-sommer/perl/magical-matrix-perl b/challenge-089/arne-sommer/perl/magical-matrix-perl
new file mode 100755
index 0000000000..a1e6e58ae7
--- /dev/null
+++ b/challenge-089/arne-sommer/perl/magical-matrix-perl
@@ -0,0 +1,37 @@
+#! /usr/bin/env perl
+
+use strict;
+use feature 'say';
+use Getopt::Long;
+use Algorithm::Combinatorics 'permutations';
+
+my $verbose = 0;
+my $all = 0;
+
+GetOptions("verbose" => \$verbose,
+ "all" => \$all);
+
+my @source = 1..9;
+
+for my $list (permutations(\@source))
+{
+ my @candidate = @$list;
+ say ":: " . join(@candidate, ", ") if $verbose;
+ my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate;
+
+ next unless $a + $b + $c == 15;
+ next unless $d + $e + $f == 15;
+ next unless $g + $h + $i == 15;
+ next unless $a + $d + $g == 15;
+ next unless $b + $e + $h == 15;
+ next unless $c + $f + $i == 15;
+ next unless $a + $e + $i == 15;
+ next unless $c + $e + $g == 15;
+
+ say "[ " . join(" ", @candidate[0..2]) . " ]";
+ say "[ " . join(" ", @candidate[3..5]) . " ]";
+ say "[ " . join(" ", @candidate[6..8]) . " ]";
+ say "" if $all;
+
+ last unless $all;
+}
diff --git a/challenge-089/arne-sommer/raku/ch-1.raku b/challenge-089/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..457db81ee6
--- /dev/null
+++ b/challenge-089/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,32 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * > 0;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose));
+
+say (1..$N).combinations(2).map( *.&gcd ).sum;
+
+sub gcd (@numbers)
+{
+ my %common = divisors(@numbers[0]) (&) divisors(@numbers[1]);
+ my @common = %common.keys.sort;
+ my $gcd = @common[* -1];
+
+ say ":: GCD(@numbers[0], @numbers[1]) -> $gcd" if $verbose;
+
+ return $gcd;
+}
+
+sub divisors ($number, :$not-self, :$not-one)
+{
+ my @divisors;
+
+ for ($not-one ?? 2 !! 1) .. $number/2 -> $candidate
+ {
+ @divisors.push: $candidate if $number %% $candidate;
+ }
+
+ @divisors.push: $number unless $not-self;
+
+ return @divisors;
+}
diff --git a/challenge-089/arne-sommer/raku/ch-2.raku b/challenge-089/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..df60aba90f
--- /dev/null
+++ b/challenge-089/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (:v(:$verbose), :a(:$all));
+
+for (1..9).permutations -> @candidate
+{
+ say ":: @candidate[]" if $verbose;
+ my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate;
+
+ next unless $a + $b + $c == 15;
+ next unless $d + $e + $f == 15;
+ next unless $g + $h + $i == 15;
+ next unless $a + $d + $g == 15;
+ next unless $b + $e + $h == 15;
+ next unless $c + $f + $i == 15;
+ next unless $a + $e + $i == 15;
+ next unless $c + $e + $g == 15;
+
+ say "[ { @candidate[0..2] } ]";
+ say "[ { @candidate[3..5] } ]";
+ say "[ { @candidate[6..8] } ]";
+ say "" if $all;
+
+ last unless $all;
+} \ No newline at end of file
diff --git a/challenge-089/arne-sommer/raku/gcd-sum b/challenge-089/arne-sommer/raku/gcd-sum
new file mode 100755
index 0000000000..457db81ee6
--- /dev/null
+++ b/challenge-089/arne-sommer/raku/gcd-sum
@@ -0,0 +1,32 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * > 0;
+
+unit sub MAIN (PositiveInt $N, :v(:$verbose));
+
+say (1..$N).combinations(2).map( *.&gcd ).sum;
+
+sub gcd (@numbers)
+{
+ my %common = divisors(@numbers[0]) (&) divisors(@numbers[1]);
+ my @common = %common.keys.sort;
+ my $gcd = @common[* -1];
+
+ say ":: GCD(@numbers[0], @numbers[1]) -> $gcd" if $verbose;
+
+ return $gcd;
+}
+
+sub divisors ($number, :$not-self, :$not-one)
+{
+ my @divisors;
+
+ for ($not-one ?? 2 !! 1) .. $number/2 -> $candidate
+ {
+ @divisors.push: $candidate if $number %% $candidate;
+ }
+
+ @divisors.push: $number unless $not-self;
+
+ return @divisors;
+}
diff --git a/challenge-089/arne-sommer/raku/magical-matrix b/challenge-089/arne-sommer/raku/magical-matrix
new file mode 100755
index 0000000000..df60aba90f
--- /dev/null
+++ b/challenge-089/arne-sommer/raku/magical-matrix
@@ -0,0 +1,25 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (:v(:$verbose), :a(:$all));
+
+for (1..9).permutations -> @candidate
+{
+ say ":: @candidate[]" if $verbose;
+ my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate;
+
+ next unless $a + $b + $c == 15;
+ next unless $d + $e + $f == 15;
+ next unless $g + $h + $i == 15;
+ next unless $a + $d + $g == 15;
+ next unless $b + $e + $h == 15;
+ next unless $c + $f + $i == 15;
+ next unless $a + $e + $i == 15;
+ next unless $c + $e + $g == 15;
+
+ say "[ { @candidate[0..2] } ]";
+ say "[ { @candidate[3..5] } ]";
+ say "[ { @candidate[6..8] } ]";
+ say "" if $all;
+
+ last unless $all;
+} \ No newline at end of file