aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-089/paulo-custodio/README1
-rw-r--r--challenge-089/paulo-custodio/forth/ch-1.fs31
-rw-r--r--challenge-089/paulo-custodio/forth/ch-2.fs89
-rw-r--r--challenge-089/paulo-custodio/perl/ch-1.pl38
-rw-r--r--challenge-089/paulo-custodio/perl/ch-2.pl77
-rw-r--r--challenge-089/paulo-custodio/test.pl29
6 files changed, 265 insertions, 0 deletions
diff --git a/challenge-089/paulo-custodio/README b/challenge-089/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-089/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-089/paulo-custodio/forth/ch-1.fs b/challenge-089/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..0189470cff
--- /dev/null
+++ b/challenge-089/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,31 @@
+\ Challenge 089
+\
+\ TASK #1 › GCD Sum
+\ Submitted by: Mohammad S Anwar
+\ You are given a positive integer $N.
+\
+\ Write a script to sum GCD of all possible unique pairs between 1 and $N.
+\
+\ This solution uses a recursive algorithm to compute the GCD.
+\
+\ Start the script with N in the stack, i.e.
+\ gforth -e 12345 ch-1.pl
+
+: gcd { a b -- gcd }
+ a 0= IF
+ b \ return b
+ ELSE
+ b a MOD a RECURSE \ recurse to return gcd(b MOD a, a)
+ THEN
+;
+
+: sum_gcd { n -- sum }
+ 0 \ push initial sum
+ n 1 ?DO \ I: 1 .. n-1
+ n 1+ I 1+ ?DO \ J: I+1 .. n
+ I J gcd + \ accumulate gcd
+ LOOP
+ LOOP
+;
+
+sum_gcd . CR BYE
diff --git a/challenge-089/paulo-custodio/forth/ch-2.fs b/challenge-089/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..bf593d1e79
--- /dev/null
+++ b/challenge-089/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,89 @@
+#! /usr/bin/env gforth
+
+\ Challenge 089
+\
+\ TASK #2 › Magical Matrix
+\ Submitted by: Mohammad S Anwar
+\ 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
+\ g + h + i = 15
+\ a + d + g = 15
+\ b + e + h = 15
+\ c + f + i = 15
+\ a + e + i = 15
+\ c + e + g = 15
+
+\ Test a brute-force method; for a better algorithm see my perl solution.
+
+: solve
+ 1 2 3 4 5 6 7 8 9 { a b c d e f g h i } \ create 9 local variables
+ 0 TO a BEGIN a 1+ TO a a 10 < WHILE
+ 0 TO b BEGIN b 1+ TO b b 10 < WHILE
+ b a <> IF
+ 0 TO c BEGIN c 1+ TO c c 10 < WHILE
+ c a <> c b <> AND IF
+ 0 TO d BEGIN d 1+ TO d d 10 < WHILE
+ d a <> d b <> d c <> AND AND IF
+ 0 TO e BEGIN e 1+ TO e e 10 < WHILE
+ e a <> a b <> e c <> e d <> AND AND AND IF
+ 0 TO f BEGIN f 1+ TO f f 10 < WHILE
+ f a <> f b <> f c <> f d <> f e <> AND AND AND AND IF
+ 0 TO g BEGIN g 1+ TO g g 10 < WHILE
+ g a <> g b <> g c <> g d <> g e <> g f <>
+ AND AND AND AND AND IF
+ 0 TO h BEGIN h 1+ TO h h 10 < WHILE
+ h a <> h b <> h c <> h d <> h e <> h f <> h g <>
+ AND AND AND AND AND AND IF
+ 0 TO i BEGIN i 1+ TO i i 10 < WHILE
+ i a <> i b <> i c <> i d <>
+ i e <> i f <> i g <> i h <>
+ AND AND AND AND AND AND AND IF
+ a b c + + 15 = IF
+ d e f + + 15 = IF
+ g h i + + 15 = IF
+ a d g + + 15 = IF
+ b e h + + 15 = IF
+ c f i + + 15 = IF
+ a e i + + 15 = IF
+ c e g + + 15 = IF
+ ." [ " a . b . c . ." ]" CR
+ ." [ " d . e . f . ." ]" CR
+ ." [ " g . h . i . ." ]" CR
+ EXIT
+ THEN
+ THEN
+ THEN
+ THEN
+ THEN
+ THEN
+ THEN
+ THEN
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ THEN
+ REPEAT
+ REPEAT
+;
+
+solve BYE
+
diff --git a/challenge-089/paulo-custodio/perl/ch-1.pl b/challenge-089/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..24c49ed8b0
--- /dev/null
+++ b/challenge-089/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+# Challenge 089
+
+# TASK #1 › GCD Sum
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N.
+#
+# Write a script to sum GCD of all possible unique pairs between 1 and $N.
+
+# This solution uses a recursive algorithm to compute the GCD.
+
+use strict;
+use warnings;
+
+sub gcd {
+ my($a, $b) = @_;
+ if ($a == 0) {
+ return $b;
+ }
+ else {
+ return gcd($b % $a, $a);
+ }
+}
+
+sub sum_gcd {
+ my($n) = @_;
+ my $sum = 0;
+ for my $a (1 .. $n-1) {
+ for my $b ($a+1 .. $n) {
+ $sum += gcd($a, $b);
+ }
+ }
+ return $sum;
+}
+
+my $n = shift;
+print sum_gcd($n), "\n";
diff --git a/challenge-089/paulo-custodio/perl/ch-2.pl b/challenge-089/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..4f8f8b137c
--- /dev/null
+++ b/challenge-089/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/env perl
+
+# Challenge 089
+
+# TASK #2 › Magical Matrix
+# Submitted by: Mohammad S Anwar
+# 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
+# g + h + i = 15
+# a + d + g = 15
+# b + e + h = 15
+# c + f + i = 15
+# a + e + i = 15
+# c + e + g = 15
+
+# Solution based on Édouard Lucas (https://en.wikipedia.org/wiki/Magic_square#A_method_for_constructing_a_magic_square_of_order_3)
+#
+# Solution is:
+#
+# [ c - b | c + (a + b) | c - a ]
+# [ c - (a - b) | c | c + (a - b) ]
+# [ c + a | c - (a + b) | c + b ]
+#
+# The magic constant is 3c, where 0 < a < b < (c - a) and b != 2a.
+# Moreover, every 3×3 magic square of distinct positive integers is of this form.
+#
+# In the problem statement it is said that the sum of each row, column and
+# diagonal is 15, this is the square magic constant, therefore we know that:
+#
+# c = 5
+#
+# We need to find the pairs (a,b) that satisfy the above conditions.
+
+use strict;
+use warnings;
+
+# return a list with the values of the magic square
+sub square {
+ my($a, $b, $c) = @_;
+ my @sq;
+ push @sq, [ $c - $b, $c + ($a + $b), $c - $a ];
+ push @sq, [ $c - ($a - $b), $c, $c + ($a - $b) ];
+ push @sq, [ $c + $a, $c - ($a + $b), $c + $b ];
+ return @sq;
+}
+
+# find a and b for a given c
+sub find_ab {
+ my($c) = @_;
+ for my $a (1 .. 3*$c-1) {
+ for my $b ($a+1 .. 3*$c) {
+ if ($b < $c-$a && $b != 2*$a) {
+ return ($a, $b);
+ }
+ }
+ }
+}
+
+my $magic_constant = 15;
+my $c = $magic_constant / 3;
+my($a, $b) = find_ab($c);
+my @sq = square($a,$b,$c);
+for (@sq) {
+ print "[ ", join(" ", @$_), " ]\n";
+}
+
+
+
+
diff --git a/challenge-089/paulo-custodio/test.pl b/challenge-089/paulo-custodio/test.pl
new file mode 100644
index 0000000000..b51643c027
--- /dev/null
+++ b/challenge-089/paulo-custodio/test.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+
+# task 1
+
+is `perl perl/ch-1.pl 3`, "3\n";
+is `perl perl/ch-1.pl 4`, "7\n";
+
+is `gforth -e 3 forth/ch-1.fs`, "3 \n";
+is `gforth -e 4 forth/ch-1.fs`, "7 \n";
+
+# task 2
+
+is `perl perl/ch-2.pl`, <<END;
+[ 2 9 4 ]
+[ 7 5 3 ]
+[ 6 1 8 ]
+END
+
+is `gforth forth/ch-2.fs`, <<END;
+[ 2 7 6 ]
+[ 9 5 1 ]
+[ 4 3 8 ]
+END
+
+done_testing;