diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-25 22:14:50 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-25 22:14:50 +0100 |
| commit | f806354488aff214f3dd3a0419b94dd73bb8acab (patch) | |
| tree | bfa26869ed9c1ca5d594f46c349ca2ad5ff8409b /challenge-109 | |
| parent | 61726267f901e536941bdee412c7007421779ffd (diff) | |
| download | perlweeklychallenge-club-f806354488aff214f3dd3a0419b94dd73bb8acab.tar.gz perlweeklychallenge-club-f806354488aff214f3dd3a0419b94dd73bb8acab.tar.bz2 perlweeklychallenge-club-f806354488aff214f3dd3a0419b94dd73bb8acab.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-109')
| -rw-r--r-- | challenge-109/colin-crain/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-109/colin-crain/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-109/colin-crain/perl/ch-2.pl | 127 | ||||
| -rw-r--r-- | challenge-109/colin-crain/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-109/colin-crain/python/ch-2.py | 92 | ||||
| -rw-r--r-- | challenge-109/colin-crain/raku/ch-1.raku | 32 | ||||
| -rw-r--r-- | challenge-109/colin-crain/raku/ch-2.raku | 127 |
7 files changed, 453 insertions, 0 deletions
diff --git a/challenge-109/colin-crain/blog.txt b/challenge-109/colin-crain/blog.txt new file mode 100644 index 0000000000..2550152193 --- /dev/null +++ b/challenge-109/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.com/2021/04/25/dividing-sums-into-four-squares/ diff --git a/challenge-109/colin-crain/perl/ch-1.pl b/challenge-109/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..3ac38aa0b9 --- /dev/null +++ b/challenge-109/colin-crain/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# chowla.pl
+#
+# TASK #1 › Chowla Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 20 Chowla Numbers, named after, Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as:
+#
+# C(n) = sum of divisors of n except 1 and n
+#
+# Output:
+# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+for (1..20, 479001600) {
+ printf "C(%d)%*s= %d\n", $_, 3-length, ' ', sum_divisors($_);
+}
+
+sub sum_divisors ($n) {
+ my $out = 0;
+ for (2..sqrt $n) {
+ unless ($n % $_) {
+ $out += ($n/$_ == $_ ? $_ : $_ + $n/$_) ;
+ }
+ }
+ return $out;
+}
+
diff --git a/challenge-109/colin-crain/perl/ch-2.pl b/challenge-109/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..d9563bf1c0 --- /dev/null +++ b/challenge-109/colin-crain/perl/ch-2.pl @@ -0,0 +1,127 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# four-sq-permute.pl
+#
+#
+# TASK #2 › Four Squares Puzzle
+# Submitted by: Mohammad S Anwar
+# You are given four squares as below with numbers named a,b,c,d,e,f,g.
+#
+# (1) (3)
+# ╔══════════════╗ ╔══════════════╗
+# ║ ║ ║ ║
+# ║ a ║ ║ e ║
+# ║ ║ (2) ║ ║ (4)
+# ║ ┌───╫──────╫───┐ ┌───╫─────────┐
+# ║ │ ║ ║ │ │ ║ │
+# ║ │ b ║ ║ d │ │ f ║ │
+# ║ │ ║ ║ │ │ ║ │
+# ║ │ ║ ║ │ │ ║ │
+# ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │
+# │ c │ │ g │
+# │ │ │ │
+# │ │ │ │
+# └──────────────┘ └─────────────┘
+# Write a script to place the given unique numbers in the square box so that sum of numbers in each box is the same.
+#
+# Example
+# Input: 1,2,3,4,5,6,7
+#
+# Output:
+#
+# a = 6
+# b = 4
+# c = 1
+# d = 5
+# e = 2
+# f = 3
+# g = 7
+#
+# Box 1: a + b = 6 + 4 = 10
+# Box 2: b + c + d = 4 + 1 + 5 = 10
+# Box 3: d + e + f = 5 + 2 + 3 = 10
+# Box 4: f + g = 3 + 7 = 10
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+use Algorithm::Combinatorics qw(permutations);
+use List::Util 1.56 qw(sum zip); ## need 1.56 for zip
+
+if (@ARGV == 7) {
+ my @sol = find_solutions(\@ARGV);
+ say "Input list: @ARGV";
+ say scalar @sol, " solutions found.\n";
+ if (@sol) {
+ report($_) for @sol;
+ }
+}
+else {
+ for my $s (-10..10) {
+ my @list = ($s..$s+6);
+
+ my @sol = find_solutions(\@list);
+ next if not @sol;
+
+ say "+++++++++++++++++++++++++++++\n";
+ say "Input list: @list";
+ say scalar @sol, " solutions found.\n";
+ report($_) for @sol;
+ }
+}
+
+sub find_solutions ($list) {
+ my @out;
+ for my $candidate ( permutations($list) ) {
+ my $n = validate($candidate);
+ push @out, [$candidate, $n] if defined $n;
+ }
+
+ return @out;
+}
+
+sub validate ($r) {
+ my $n = $r->[0] + $r->[1];
+ return $n if $n
+ == $r->[1] + $r->[2] + $r->[3]
+ == $r->[3] + $r->[4] + $r->[5]
+ == $r->[5] + $r->[6] ;
+ return undef;
+}
+
+sub report ($sol) {
+ my ($list, $num) = $sol->@*;
+
+ say<<~"HEAD";
+ ===============================
+ Solution:
+ Sum: $num for all squares
+ Values:
+ HEAD
+
+ my @lets = zip ['a'..'g'], $list;
+ for (@lets) {
+ say "\t\t$_->[0] = $_->[1]";
+ }
+
+ say<<~"TAIL";
+
+ ===============================
+ TAIL
+}
+
+
+
+
+
+
+
diff --git a/challenge-109/colin-crain/python/ch-1.py b/challenge-109/colin-crain/python/ch-1.py new file mode 100644 index 0000000000..554f5b5518 --- /dev/null +++ b/challenge-109/colin-crain/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3
+#
+#
+# chowla.py
+#
+# TASK #1 › Chowla Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 20 Chowla Numbers, named after, Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as:
+#
+# C(n) = sum of divisors of n except 1 and n
+#
+# Output:
+# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+import math
+
+def sum_divisors (n):
+ out = 0
+ for d in range( 2, int(math.sqrt(n))+1 ):
+ id = divmod(n,d)
+ if id[1] == 0:
+ out += d
+ if id[0] != d:
+ out += id[0]
+ return out
+
+
+for n in range(1,21):
+ print(f"C({n}) = ", sum_divisors(n))
+
+
diff --git a/challenge-109/colin-crain/python/ch-2.py b/challenge-109/colin-crain/python/ch-2.py new file mode 100644 index 0000000000..3d45b655a7 --- /dev/null +++ b/challenge-109/colin-crain/python/ch-2.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3
+#
+#
+# four-sq-permute.py
+#
+# TASK #2 › Four Squares Puzzle
+# Submitted by: Mohammad S Anwar
+# You are given four squares as below with numbers named a,b,c,d,e,f,g.
+#
+# (1) (3)
+# ╔══════════════╗ ╔══════════════╗
+# ║ ║ ║ ║
+# ║ a ║ ║ e ║
+# ║ ║ (2) ║ ║ (4)
+# ║ ┌───╫──────╫───┐ ┌───╫─────────┐
+# ║ │ ║ ║ │ │ ║ │
+# ║ │ b ║ ║ d │ │ f ║ │
+# ║ │ ║ ║ │ │ ║ │
+# ║ │ ║ ║ │ │ ║ │
+# ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │
+# │ c │ │ g │
+# │ │ │ │
+# │ │ │ │
+# └──────────────┘ └─────────────┘
+# Write a script to place the given unique numbers in the square box so that sum of numbers in each box is the same.
+#
+# Example
+# Input: 1,2,3,4,5,6,7
+#
+# Output:
+#
+# a = 6
+# b = 4
+# c = 1
+# d = 5
+# e = 2
+# f = 3
+# g = 7
+#
+# Box 1: a + b = 6 + 4 = 10
+# Box 2: b + c + d = 4 + 1 + 5 = 10
+# Box 3: d + e + f = 5 + 2 + 3 = 10
+# Box 4: f + g = 3 + 7 = 10
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+from itertools import permutations
+
+
+
+def find_solutions ( lst ):
+ out = []
+ for candidate in list( permutations(lst) ):
+ (v, n) = validate( candidate )
+ if v:
+ out.append( [candidate, n] )
+ return out
+
+def validate ( lst ):
+ n = lst[0] + lst[1]
+ return ( (n == lst[1] + lst[2] + lst[3]
+ == lst[3] + lst[4] + lst[5]
+ == lst[5] + lst[6]), n )
+
+def report ( tup ):
+ (list, n) = tup
+
+ print(f'''
+ ===============================
+ solution
+ sum is {n}
+ values:
+ ''')
+
+ letter_values = zip( ['a','b','c','d','e','f','g'] , list )
+ for lv in letter_values:
+ print(f"\t\t{lv[0]} = {lv[1]}")
+
+ print(f'''
+ ===============================
+ ''')
+
+input = [1,2,3,4,5,6,7]
+solution = find_solutions(input)
+if len(solution) > 0:
+ for sol in solution:
+ report(sol)
diff --git a/challenge-109/colin-crain/raku/ch-1.raku b/challenge-109/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..652bae7857 --- /dev/null +++ b/challenge-109/colin-crain/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env perl6 +# +# +# chowla.raku +# +# TASK #1 › Chowla Numbers +# Submitted by: Mohammad S Anwar +# Write a script to generate first 20 Chowla Numbers, named after, Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as: +# +# C(n) = sum of divisors of n except 1 and n +# +# Output: +# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN () ; + +for 1..20 { + printf "C(%d)%*s= %s\n", $_, 3 - $_.chars, ' ', sum_divisors($_); +} + +sub sum_divisors ($n) { + [+] (2..sqrt $n).grep({$n %% $_}) + .map({$n/$_ == $_ ?? $_ + !! $_ + $n/$_}) +} + diff --git a/challenge-109/colin-crain/raku/ch-2.raku b/challenge-109/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..6ecdec16fa --- /dev/null +++ b/challenge-109/colin-crain/raku/ch-2.raku @@ -0,0 +1,127 @@ +#!/usr/bin/env perl6 +# +# +# four-square-permute.raku +# +# TASK #2 › Four Squares Puzzle +# Submitted by: Mohammad S Anwar +# You are given four squares as below with numbers named a,b,c,d,e,f,g. +# +# (1) (3) +# ╔══════════════╗ ╔══════════════╗ +# ║ ║ ║ ║ +# ║ a ║ ║ e ║ +# ║ ║ (2) ║ ║ (4) +# ║ ┌───╫──────╫───┐ ┌───╫─────────┐ +# ║ │ ║ ║ │ │ ║ │ +# ║ │ b ║ ║ d │ │ f ║ │ +# ║ │ ║ ║ │ │ ║ │ +# ║ │ ║ ║ │ │ ║ │ +# ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ +# │ c │ │ g │ +# │ │ │ │ +# │ │ │ │ +# └──────────────┘ └─────────────┘ +# Write a script to place the given unique numbers in the square box so that sum of numbers in each box is the same. +# +# Example +# Input: 1,2,3,4,5,6,7 +# +# Output: +# +# a = 6 +# b = 4 +# c = 1 +# d = 5 +# e = 2 +# f = 3 +# g = 7 +# +# Box 1: a + b = 6 + 4 = 10 +# Box 2: b + c + d = 4 + 1 + 5 = 10 +# Box 3: d + e + f = 5 + 2 + 3 = 10 +# Box 4: f + g = 3 + 7 = 10 +# +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +multi sub MAIN () { + for 1..1 -> $s { + my @list = ($s..$s+6); + + my @sol = find_solutions(@list); + next if not @sol.elems; + + say "+++++++++++++++++++++++++++++\n"; + say "Input list: ", @list; + say @sol.elems, " solutions found.\n"; + report($_) for @sol; + } +} + +multi sub MAIN (*@list where { @list.elems == 7 }) { + my @sol = find_solutions(@list); + say "Input list: ", @list.sort; + say @sol.elems, " solutions found"; + if (@sol.elems) { + report($_) for @sol; + } +} + +multi sub MAIN (*@list) { + say "please enter 7 numbers"; +} + +sub find_solutions (@list) { + my @out; + my @pm = @list.permutations; + + for @pm -> $candidate { + my $n = validate($candidate); + push @out, ($candidate, $n) if defined $n; + } + + return @out; +} + +sub validate (@list) { + my $n = @list[0] + @list[1]; + $n == @list[1] + @list[2] + @list[3] + == @list[3] + @list[4] + @list[5] + == @list[5] + @list[6] ?? $n + !! Nil +} + +sub report ($sol) { + + my ($list, $num) = |$sol; + say "list ", $list; + say "total ", $num; + + + say qq:to/HEAD/; + + =============================== + Solution: + Sum: $num for all squares + Values: + HEAD + + my @pairs = <a b c d e f g> Z $list.list; + + for @pairs { + say "\t\t $_[0] = $_[1]"; + } + + say qq:to/TAIL/; + + =============================== + TAIL +} + + |
