aboutsummaryrefslogtreecommitdiff
path: root/challenge-089
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-12-06 14:00:09 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-12-06 14:00:09 +0000
commit18c9f8c56c08a0062049b118a62a7b0386da850c (patch)
tree01d66c5488fa50c725647769116a6e89c7448349 /challenge-089
parent33f2b9c7ca303cec3920728831ba663e2982c204 (diff)
parent09e99fd07ab3d5eca1ae4a51f706c01929f54328 (diff)
downloadperlweeklychallenge-club-18c9f8c56c08a0062049b118a62a7b0386da850c.tar.gz
perlweeklychallenge-club-18c9f8c56c08a0062049b118a62a7b0386da850c.tar.bz2
perlweeklychallenge-club-18c9f8c56c08a0062049b118a62a7b0386da850c.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-089')
-rw-r--r--challenge-089/wanderdoc/perl/ch-1.pl52
-rw-r--r--challenge-089/wanderdoc/perl/ch-2.pl106
2 files changed, 158 insertions, 0 deletions
diff --git a/challenge-089/wanderdoc/perl/ch-1.pl b/challenge-089/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..a368659e8a
--- /dev/null
+++ b/challenge-089/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a positive integer $N. Write a script to sum GCD of all possible unique pairs between 1 and $N.
+Example 1: Input: 3 Output: 3 (gcd(1,2) + gcd(1,3) + gcd(2,3))
+Example 2: Input: 4 Output: 7 (gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4))
+
+=cut
+
+
+
+
+
+
+use Algorithm::Combinatorics qw(combinations);
+use Test::More;
+
+sub sum_gcd
+{
+ my $n = $_[0];
+ return 1 if $n == 1;
+
+ die "Positive integer needed!$/" if ( $n < 1 or $n != int($n));
+
+ my $sum = 0;
+ my $iter = combinations([1 .. $n], 2);
+ while (my $p = $iter->next)
+ {
+ $sum += gcd(@$p);
+ }
+ return $sum;
+}
+
+sub gcd
+{
+ my ($num_1, $num_2) = @_;
+
+
+ while ( $num_1 != $num_2 )
+ {
+ ($num_1, $num_2) = ($num_2, $num_1) if ($num_2 > $num_1);
+
+ ($num_1, $num_2) = ($num_1 - $num_2, $num_2);
+ }
+ return $num_2;
+}
+
+is(sum_gcd(3), 3, 'Example 1');
+is(sum_gcd(4), 7, 'Example 2');
+done_testing(); \ No newline at end of file
diff --git a/challenge-089/wanderdoc/perl/ch-2.pl b/challenge-089/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..4902944e9c
--- /dev/null
+++ b/challenge-089/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,106 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+Write a script to display matrix as below with numbers 1 - 9. Please make sure numbers are used once.
+[ a b c ]
+[ d e f ]
+[ g h i ]
+So that it satisfies the following: a + b + c = 15 d + e + f = 15 + h + i = 15 a + d + g = 15 b + e + h = 15
+c + f + i = 15 a + e + i = 15 c + e + g = 15
+=cut
+
+
+use List::Util qw(sum all);
+
+my $n = 3;
+my $magic = $n * ($n * $n + 1)/ 2; # 15 for 3x3 matrix.
+my $aref = find($n);
+
+print_mtr($aref) if check($aref, $magic);
+
+sub find
+{
+ my $n = $_[0];
+
+ my $aref = _fill($n);
+ my $r = 0;
+ my $c = int $n/2;
+ my $counter++;
+ while ( grep { not defined($_)} map {@$_} @$aref )
+ {
+ $aref->[$r][$c] = $counter++;
+
+ if ( ($r - 1) < 0 and ($c - 1) < 0 )
+ {
+ $r++;
+ }
+
+
+ elsif ( ($r - 1) < 0 )
+ {
+ $r = $n - 1;
+
+ $c--;
+ }
+ elsif ( ($c - 1) < 0 )
+ {
+
+ $c = $n - 1;
+ $r--;
+ }
+ elsif ( $aref->[$r - 1][$c - 1])
+ {
+ $r++;
+ }
+ else
+ {
+ $r--;
+ $c--;
+ }
+ }
+ return $aref;
+}
+
+
+
+sub check
+{
+ my ($aref, $check) = @_;
+ my $rows = all { sum(@$_) == $check } @$aref;
+ my $cols = all {$_ == 1}
+ (map{ my $i = $_; all{sum( map{ $_->[$i]} @$aref ) == $check} }
+ 0 .. $#$aref);
+
+
+ my $diag_1 = sum( map { $aref->[$_][$_] } 0 .. $#$aref) == $check;
+ my $diag_2 = sum( map { $aref->[$_][$#$aref - $_] } 0 .. $#$aref) == $check;
+ return ($rows and $cols and $diag_1 and $diag_2);
+}
+
+sub print_mtr
+{
+ my $aref = $_[0];
+
+ for my $i ( 0 .. $#$aref )
+ {
+ print join(' ', '[', join(" ", @{$aref->[$i]}), ']'), $/;
+ }
+}
+
+sub _fill
+{
+
+ my $n = $_[0]; my $aref;
+ for my $i ( 0 .. $n - 1 )
+ {
+ for my $j ( 0 .. $n - 1 )
+ {
+ $aref->[$i][$j] = undef;
+ }
+
+ }
+
+ return $aref;
+} \ No newline at end of file