aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-089/adam-russell/blog.txt1
-rw-r--r--challenge-089/adam-russell/blog1.txt1
-rw-r--r--challenge-089/adam-russell/cxx/ch-1.cxx0
-rw-r--r--challenge-089/adam-russell/cxx/ch-2.cxx0
-rw-r--r--challenge-089/adam-russell/perl/ch-1.pl46
-rw-r--r--challenge-089/adam-russell/perl/ch-2.pl40
-rw-r--r--challenge-089/adam-russell/prolog/ch-1.p37
-rw-r--r--challenge-089/adam-russell/prolog/ch-2.p43
8 files changed, 168 insertions, 0 deletions
diff --git a/challenge-089/adam-russell/blog.txt b/challenge-089/adam-russell/blog.txt
index e69de29bb2..52441b7561 100644
--- a/challenge-089/adam-russell/blog.txt
+++ b/challenge-089/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2020/12/06
diff --git a/challenge-089/adam-russell/blog1.txt b/challenge-089/adam-russell/blog1.txt
new file mode 100644
index 0000000000..4b12cf010a
--- /dev/null
+++ b/challenge-089/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2020/12/06
diff --git a/challenge-089/adam-russell/cxx/ch-1.cxx b/challenge-089/adam-russell/cxx/ch-1.cxx
deleted file mode 100644
index e69de29bb2..0000000000
--- a/challenge-089/adam-russell/cxx/ch-1.cxx
+++ /dev/null
diff --git a/challenge-089/adam-russell/cxx/ch-2.cxx b/challenge-089/adam-russell/cxx/ch-2.cxx
deleted file mode 100644
index e69de29bb2..0000000000
--- a/challenge-089/adam-russell/cxx/ch-2.cxx
+++ /dev/null
diff --git a/challenge-089/adam-russell/perl/ch-1.pl b/challenge-089/adam-russell/perl/ch-1.pl
index e69de29bb2..999052ff68 100644
--- a/challenge-089/adam-russell/perl/ch-1.pl
+++ b/challenge-089/adam-russell/perl/ch-1.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+##
+# You are given a positive integer $N. Write a script to sum GCD of all possible
+# unique pairs between 1 and $N.
+##
+
+sub all_unique_pairs{
+ my($n) = @_;
+ my %pairs;
+ for my $i (1 .. $n){
+ for my $j (1 .. $n){
+ $pairs{"$i-$j"} = -1 unless $pairs{"$i-$j"} || $pairs{"$j-$i"} || $i == $j;
+ }
+ }
+ return sort keys %pairs;
+}
+
+sub euclid {
+ my($a, $b) = @_;
+ return ($b) ? euclid($b, $a % $b) : $a;
+}
+
+MAIN:{
+ my $gcd_sum = 0;
+ my @values = all_unique_pairs(3);
+ for my $pair (@values[0 .. @values - 2]){
+ my($i, $j) = split(/-/, $pair);
+ $gcd_sum += euclid($i, $j);
+ print "gcd($i, $j) + ";
+ }
+ my ($i, $j) = split(/-/, $values[-1]);
+ $gcd_sum += euclid($i, $j);
+ print "gcd($i, $j) = $gcd_sum\n";
+
+ $gcd_sum = 0;
+ @values = all_unique_pairs(4);
+ for my $pair (@values[0 .. @values - 2]){
+ my($i, $j) = split(/-/, $pair);
+ $gcd_sum += euclid($i, $j);
+ print "gcd($i, $j) + ";
+ }
+ ($i, $j) = split(/-/, $values[-1]);
+ $gcd_sum += euclid($i, $j);
+ print "gcd($i, $j) = $gcd_sum\n";
+} \ No newline at end of file
diff --git a/challenge-089/adam-russell/perl/ch-2.pl b/challenge-089/adam-russell/perl/ch-2.pl
index e69de29bb2..a427d8169e 100644
--- a/challenge-089/adam-russell/perl/ch-2.pl
+++ b/challenge-089/adam-russell/perl/ch-2.pl
@@ -0,0 +1,40 @@
+use strict;
+use warnings;
+##
+# Write a script to display matrix as below with numbers 1 - 9.
+# Please make sure numbers are used once.
+##
+use boolean;
+use Math::GSL::Permutation q/:all/;
+
+sub validate {
+ my($a, $b, $c, $d, $e, $f, $g, $h, $i) = @_;
+ return false if ($a + $b + $c) != 15;
+ return false if ($d + $e + $f) != 15;
+ return false if ($g + $h + $i) != 15;
+ return false if ($a + $d + $g) != 15;
+ return false if ($b + $e + $h) != 15;
+ return false if ($c + $f + $i) != 15;
+ return false if ($a + $e + $i) != 15;
+ return false if ($c + $e + $g) != 15;
+ return true;
+}
+
+sub print_matrix {
+ my($a, $b, $c, $d, $e, $f, $g, $h, $i) = @_;
+ print "[ $a $b $c ]\n";
+ print "[ $d $e $f ]\n";
+ print "[ $g $h $i ]\n";
+}
+
+MAIN:{
+ my $permutation = new Math::GSL::Permutation(9);
+ while(gsl_permutation_next($permutation->raw) == 0){
+ my @values = $permutation->as_list();
+ @values = map { $_ + 1 } @values;
+ do {
+ print_matrix(@values);
+ print "\n";
+ }if validate(@values);
+ }
+} \ No newline at end of file
diff --git a/challenge-089/adam-russell/prolog/ch-1.p b/challenge-089/adam-russell/prolog/ch-1.p
index e69de29bb2..1d1a9a7146 100644
--- a/challenge-089/adam-russell/prolog/ch-1.p
+++ b/challenge-089/adam-russell/prolog/ch-1.p
@@ -0,0 +1,37 @@
+range_list(0, [0]).
+range_list(N, List):-
+ range_list(N, [], List).
+range_list(0, List, List).
+range_list(N, ListAccum, List):-
+ N0 is N - 1,
+ range_list(N0, [N|ListAccum], List).
+
+unique_pairs(List, Pairs):-
+ unique_pairs(List, List, [], Pairs).
+
+unique_pairs([], [], Pairs, Pairs).
+unique_pairs([_|T0], [], PairsAccum, Pairs):-
+ unique_pairs(T0, T0, PairsAccum, Pairs).
+unique_pairs([H0|T0], [H1|T1], PairsAccum, Pairs):-
+ \+ member([H0, H1], PairsAccum),
+ \+ member([H1, H0], PairsAccum),
+ H0 \= H1,
+ unique_pairs([H0|T0], T1, [[H0, H1]|PairsAccum], Pairs).
+unique_pairs([H0|T0], [_|T1], PairsAccum, Pairs):-
+ unique_pairs([H0|T0], T1, PairsAccum, Pairs).
+
+write_gcd_pairs(Pairs):-
+ write_gcd_pairs(Pairs, 0).
+write_gcd_pairs([[I,J]|[]], Sum):-
+ Sum0 is Sum + gcd(I, J),
+ format("gcd(~d, ~d) = ~d ~n", [I, J, Sum0]).
+write_gcd_pairs([[I,J]|T], Sum):-
+ format("gcd(~d, ~d) + ", [I, J]),
+ Sum0 is Sum + gcd(I, J),
+ write_gcd_pairs(T, Sum0).
+
+main:-
+ range_list(4, L),
+ unique_pairs(L, Pairs),
+ write_gcd_pairs(Pairs),
+ halt. \ No newline at end of file
diff --git a/challenge-089/adam-russell/prolog/ch-2.p b/challenge-089/adam-russell/prolog/ch-2.p
index e69de29bb2..bebeaeda06 100644
--- a/challenge-089/adam-russell/prolog/ch-2.p
+++ b/challenge-089/adam-russell/prolog/ch-2.p
@@ -0,0 +1,43 @@
+all_unique(_, []).
+all_unique(L, [V|T]) :-
+ fd_exactly(1, L, V),
+ all_unique(L, T).
+
+pwc_matrix(R1,R2,R3) :-
+ R1 = [A, B, C],
+ R2 = [D, E, F],
+ R3 = [G, H, I],
+ /* each element is a number from 1 to 9 */
+ fd_domain([A, B, C], 1, 9),
+ fd_domain([D, E, F], 1, 9),
+ fd_domain([G, H, I], 1, 9),
+ /* ensure each element is unique */
+ all_unique([A, B, C, D, E, F, G, H, I], [1, 2, 3, 4, 5, 6, 7, 8, 9]),
+ /* row constraints */
+ A + B + C #= 15,
+ D + E + F #= 15,
+ G + H + I #= 15,
+ /* column constraints */
+ A + D + G #= 15,
+ B + E + H #= 15,
+ C + F + I #= 15,
+ /* diagonal constraints */
+ A + E + I #= 15,
+ C + E + G #= 15,
+ /* label all variables to instantiate one solution */
+ fd_labeling([A, B, C, D, E, F, G, H, I]).
+
+write_rows([]).
+write_rows([H|T]):-
+ format('[ ~d ~d ~d ]~n', H),
+ write_rows(T).
+
+write_solutions([]).
+write_solutions([H|T]):-
+ write_rows(H),
+ nl,
+ write_solutions(T).
+
+main :-
+ findall([R1,R2,R3], pwc_matrix(R1,R2,R3), Solutions),
+ write_solutions(Solutions). \ No newline at end of file