diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-07 03:48:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-07 03:48:57 +0000 |
| commit | 3fd80c63123e9eaebdf4f678fef077e36dbd6a4d (patch) | |
| tree | 9811dfcca77dada9e1af60242c8a4cd782a40a5d | |
| parent | 5ee1055b3666dd11aa52c18a2228514f96f7568b (diff) | |
| parent | 677b5c60fdf816690c1496bfdb0dcbb40aa3f1ce (diff) | |
| download | perlweeklychallenge-club-3fd80c63123e9eaebdf4f678fef077e36dbd6a4d.tar.gz perlweeklychallenge-club-3fd80c63123e9eaebdf4f678fef077e36dbd6a4d.tar.bz2 perlweeklychallenge-club-3fd80c63123e9eaebdf4f678fef077e36dbd6a4d.zip | |
Merge pull request #2930 from adamcrussell/challenge-089
Challenge 089
| -rw-r--r-- | challenge-089/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-089/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-089/adam-russell/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-089/adam-russell/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-089/adam-russell/prolog/ch-1.p | 37 | ||||
| -rw-r--r-- | challenge-089/adam-russell/prolog/ch-2.p | 43 |
6 files changed, 168 insertions, 0 deletions
diff --git a/challenge-089/adam-russell/blog.txt b/challenge-089/adam-russell/blog.txt new file mode 100644 index 0000000000..52441b7561 --- /dev/null +++ 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/perl/ch-1.pl b/challenge-089/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..999052ff68 --- /dev/null +++ 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 new file mode 100644 index 0000000000..a427d8169e --- /dev/null +++ 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 new file mode 100644 index 0000000000..1d1a9a7146 --- /dev/null +++ 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 new file mode 100644 index 0000000000..bebeaeda06 --- /dev/null +++ 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 |
