aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-109/paulo-custodio/perl/ch-1.pl46
-rwxr-xr-xchallenge-109/paulo-custodio/perl/ch-2.pl65
-rw-r--r--challenge-109/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-109/paulo-custodio/t/test-2.yaml12
-rwxr-xr-xchallenge-109/paulo-custodio/test.pl7
5 files changed, 135 insertions, 0 deletions
diff --git a/challenge-109/paulo-custodio/perl/ch-1.pl b/challenge-109/paulo-custodio/perl/ch-1.pl
new file mode 100755
index 0000000000..edb3e9769d
--- /dev/null
+++ b/challenge-109/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# Challenge 109
+#
+# 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
+# NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40].
+# Output:
+# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
+
+use Modern::Perl;
+
+first_chowla(shift || 20);
+
+sub first_chowla {
+ my($num) = @_;
+ for my $i (1..$num) {
+ print ", " if $i > 1;
+ print chowla($i);
+ }
+ print "\n";
+}
+
+sub chowla {
+ my($n) = @_;
+ my @terms = grep {$_!=1 && $_!=$n} divisors($n);
+ my $sum = 0; $sum += $_ for @terms;
+ return $sum;
+}
+
+sub divisors {
+ my($n) = @_;
+ my(@div_low, @div_high);
+ for (my $i = 1; $i <= sqrt($n); $i++) {
+ if ($n%$i == 0) {
+ push @div_low, $i;
+ unshift @div_high, $n/$i if $n/$i != $i;
+ }
+ }
+ return (@div_low, @div_high);
+}
diff --git a/challenge-109/paulo-custodio/perl/ch-2.pl b/challenge-109/paulo-custodio/perl/ch-2.pl
new file mode 100755
index 0000000000..b168a71b1a
--- /dev/null
+++ b/challenge-109/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+
+# Challenge 109
+#
+# 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
+
+use Modern::Perl;
+use Math::Combinatorics;
+
+@ARGV==7 or die "Usage: ch-2.pl Nx7\n";
+my @result = place_numbers(@ARGV);
+for my $i (0..6) {
+ say chr(ord('a')+$i)," = ",$result[$i];
+}
+
+# Note: return first solution found, not necessarily same as example
+sub place_numbers {
+ my(@n) = @_;
+ my $combinat = Math::Combinatorics->new(count => 7, data => \@n);
+ while(my @permut = $combinat->next_permutation) {
+ my($a,$b,$c,$d,$e,$f,$g) = @permut;
+ my $sum = $a + $b;
+ return @permut if $b + $c + $d == $sum &&
+ $d + $e + $f == $sum &&
+ $f + $g == $sum;
+ }
+}
diff --git a/challenge-109/paulo-custodio/t/test-1.yaml b/challenge-109/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..8dc7b756b3
--- /dev/null
+++ b/challenge-109/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 20
+ input:
+ output: 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
diff --git a/challenge-109/paulo-custodio/t/test-2.yaml b/challenge-109/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..a06174efc9
--- /dev/null
+++ b/challenge-109/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,12 @@
+- setup:
+ cleanup:
+ args: 1 2 3 4 5 6 7
+ input:
+ output: |
+ a = 3
+ b = 7
+ c = 2
+ d = 1
+ e = 5
+ f = 4
+ g = 6
diff --git a/challenge-109/paulo-custodio/test.pl b/challenge-109/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-109/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';