aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-25 21:47:04 +0100
committerGitHub <noreply@github.com>2021-04-25 21:47:04 +0100
commitd6fde43c7c29692b5de469b1df4f24fce69a6776 (patch)
treef48e500860e9c1699a34cd025495146875cdf444
parentf7c2a775b6935551d74f3701eccd18a52b5fb882 (diff)
parentb4ee212c0029aa0aedbefdf161fc9700349b236a (diff)
downloadperlweeklychallenge-club-d6fde43c7c29692b5de469b1df4f24fce69a6776.tar.gz
perlweeklychallenge-club-d6fde43c7c29692b5de469b1df4f24fce69a6776.tar.bz2
perlweeklychallenge-club-d6fde43c7c29692b5de469b1df4f24fce69a6776.zip
Merge pull request #3955 from adamcrussell/challenge-109
Challenge 109
-rw-r--r--challenge-109/adam-russell/blog.txt1
-rw-r--r--challenge-109/adam-russell/blog1.txt1
-rw-r--r--challenge-109/adam-russell/perl/ch-1.pl29
-rw-r--r--challenge-109/adam-russell/perl/ch-2.pl62
-rw-r--r--challenge-109/adam-russell/prolog/ch-1.p42
-rw-r--r--challenge-109/adam-russell/prolog/ch-2.p82
6 files changed, 217 insertions, 0 deletions
diff --git a/challenge-109/adam-russell/blog.txt b/challenge-109/adam-russell/blog.txt
new file mode 100644
index 0000000000..d6493ed274
--- /dev/null
+++ b/challenge-109/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/04/25
diff --git a/challenge-109/adam-russell/blog1.txt b/challenge-109/adam-russell/blog1.txt
new file mode 100644
index 0000000000..fdf5d4d2ea
--- /dev/null
+++ b/challenge-109/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/04/25
diff --git a/challenge-109/adam-russell/perl/ch-1.pl b/challenge-109/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..e5b82ec61f
--- /dev/null
+++ b/challenge-109/adam-russell/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+##
+# Write a script to generate first 20 Chowla Numbers.
+# C(n) = sum of divisors of n except 1 and n
+##
+use constant CHOWLA_COUNT => 20;
+sub factor{
+ my($n) = @_;
+ my @factors = ();
+ foreach my $j (2..sqrt($n)){
+ push @factors, $j if $n % $j == 0;
+ push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n;
+ }
+ return @factors;
+}
+
+sub chowla{
+ my(@factors) = @_;
+ my $sum = unpack("%32I*", pack("I*", @factors));
+}
+
+MAIN:{
+ my @chowla_numbers;
+ for my $n (1 .. CHOWLA_COUNT){
+ push @chowla_numbers, chowla(factor($n));
+ }
+ print join(", ", @chowla_numbers) . "\n";
+} \ No newline at end of file
diff --git a/challenge-109/adam-russell/perl/ch-2.pl b/challenge-109/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..3a57cfebd1
--- /dev/null
+++ b/challenge-109/adam-russell/perl/ch-2.pl
@@ -0,0 +1,62 @@
+use strict;
+use warnings;
+##
+# You are given four squares as below with numbers named a,b,c,d,e,f,g.
+# to place the given unique numbers in the square box so that sum of
+# numbers in each box is the same.
+# (1) (3)
+# +--------------+ +--------------+
+# ¦ ¦ ¦ ¦
+# ¦ a ¦ ¦ e ¦
+# ¦ ¦ (2) ¦ ¦ (4)
+# ¦ +---+------+---+ +---+---------+
+# ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+# ¦ ¦ b ¦ ¦ d ¦ ¦ f ¦ ¦
+# ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+# ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+# +----------+---+ +---+------+---+ ¦
+# ¦ c ¦ ¦ g ¦
+# ¦ ¦ ¦ ¦
+# ¦ ¦ ¦ ¦
+# +--------------+ +-------------+
+##
+use AI::Prolog;
+
+my $prolog = do{
+ local $/;
+ <DATA>;
+};
+$prolog = new AI::Prolog($prolog);
+$prolog->query("sums_in_squares([1,2,3,4,5,6,7], Squares).");
+
+my $result;
+print join("\t", "a" .. "g") . "\n";
+while ($result = $prolog->results()){
+ print join("\t", @{$result->[2]}) . "\n";
+}
+
+__DATA__
+member(X,[X|T]).
+member(X,[H|T]):- member(X,T).
+sums_in_squares(Numbers, [A, B, C, D, E, F, G]):-
+ member(A, Numbers),
+ member(B, Numbers),
+ member(C, Numbers),
+ member(D, Numbers),
+ member(E, Numbers),
+ member(F, Numbers),
+ member(G, Numbers),
+ A \= B, A \= C, A \= D, A \= E, A \= F, A \= G,
+ B \= A, B \= C, B \= D, B \= E, B \= F, B \= G,
+ C \= A, C \= B, C \= D, C \= E, C \= F, C \= G,
+ D \= A, D \= B, D \= C, D \= E, D \= F, D \= G,
+ E \= A, E \= B, E \= C, E \= D, E \= F, E \= G,
+ F \= A, F \= B, F \= C, F \= D, F \= E, F \= G,
+ G \= A, G \= B, G \= C, G \= D, G \= E, G \= F,
+ Box1 is A + B,
+ Box2 is B + C + D,
+ Box3 is D + E + F,
+ Box4 is F + G,
+ Box1 == Box2,
+ Box2 == Box3,
+ Box3 == Box4.
diff --git a/challenge-109/adam-russell/prolog/ch-1.p b/challenge-109/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..3e2458293b
--- /dev/null
+++ b/challenge-109/adam-russell/prolog/ch-1.p
@@ -0,0 +1,42 @@
+/*
+ Write a script to generate first 20 Chowla Numbers.
+ C(n) = sum of divisors of n except 1 and n
+*/
+
+:-initialization(main).
+
+print_with_comma([H|[]]):-
+ write(H), nl.
+print_with_comma([H|T]):-
+ write(H),
+ write(', '),
+ print_with_comma(T).
+
+factor(N, Factors):-
+ S is round(sqrt(N)),
+ fd_domain(X, 2, S),
+ R #= N rem X,
+ R #= 0,
+ Q #= N // X,
+ Q #\= X,
+ fd_labeling([Q, X]),
+ Factors = [Q, X].
+factor(N, Factors):-
+ S is round(sqrt(N)),
+ fd_domain(X, 2, S),
+ R #= N rem X,
+ R #= 0,
+ Q #= N // X,
+ Q #= X,
+ fd_labeling([Q]),
+ Factors = [Q].
+
+chowla(ChowlaNumber):-
+ between(1, 20, N),
+ findall(F, factor(N, F), Fs),
+ flatten(Fs, Factors),
+ sum_list(Factors, ChowlaNumber).
+
+main:-
+ findall(ChowlaNumber, chowla(ChowlaNumber), ChowlaNumbers),
+ print_with_comma(ChowlaNumbers). \ No newline at end of file
diff --git a/challenge-109/adam-russell/prolog/ch-2.p b/challenge-109/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..3f7aafd6d6
--- /dev/null
+++ b/challenge-109/adam-russell/prolog/ch-2.p
@@ -0,0 +1,82 @@
+/*
+ You are given four squares as below with numbers named a,b,c,d,e,f,g.
+ to place the given unique numbers in the square box so that sum of
+ numbers in each box is the same.
+ (1) (3)
+ +--------------+ +--------------+
+ ¦ ¦ ¦ ¦
+ ¦ a ¦ ¦ e ¦
+ ¦ ¦ (2) ¦ ¦ (4)
+ ¦ +---+------+---+ +---+---------+
+ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+ ¦ ¦ b ¦ ¦ d ¦ ¦ f ¦ ¦
+ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
+ +----------+---+ +---+------+---+ ¦
+ ¦ c ¦ ¦ g ¦
+ ¦ ¦ ¦ ¦
+ ¦ ¦ ¦ ¦
+ +--------------+ +-------------+
+*/
+
+:-initialization(main).
+
+print_with_tab([H|[]]):-
+ write(H), nl.
+print_with_tab([H|T]):-
+ write(H),
+ write('\t'),
+ print_with_tab(T).
+
+print_values([]).
+print_values([H|T]):-
+ print_with_tab(H),
+ print_values(T).
+
+print_solutions(Solutions):-
+ print_with_tab([a, b, c, d, e, f, g]),
+ print_values(Solutions).
+
+all_unique(_, []).
+all_unique(L, [V|T]) :-
+ fd_exactly(1, L, V),
+ all_unique(L, T).
+
+sums_in_squares_naive(Numbers, [A, B, C, D, E, F, G]):-
+ member(A, Numbers),
+ member(B, Numbers),
+ member(C, Numbers),
+ member(D, Numbers),
+ member(E, Numbers),
+ member(F, Numbers),
+ member(G, Numbers),
+ \+ (A == B; A == C; A == D; A == E; A == F; A == G),
+ \+ (B == A; B == C; B == D; B == E; B == F; B == G),
+ \+ (C == A; C == B; C == D; C == E; C == F; C == G),
+ \+ (D == A; D == B; D == C; D == E; D == F; D == G),
+ \+ (E == A; E == B; E == C; E == D; E == F; E == G),
+ \+ (F == A; F == B; F == C; F == D; F == E; F == G),
+ \+ (G == A; G == B; G == C; G == D; G == E; G == F),
+ Box1 is A + B,
+ Box2 is B + C + D,
+ Box3 is D + E + F,
+ Box4 is F + G,
+ Box1 == Box2,
+ Box2 == Box3,
+ Box3 == Box4.
+
+sums_in_squares_fd(Numbers, [A, B, C, D, E, F, G]):-
+ fd_domain([A, B, C, D, E, F, G], Numbers),
+ all_unique([A, B, C, D, E, F, G], Numbers),
+ Box1 = A + B,
+ Box2 = B + C + D,
+ Box3 = D + E + F,
+ Box4 = F + G,
+ Box1 #= Box2,
+ Box2 #= Box3,
+ Box3 #= Box4,
+ fd_labeling([A, B, C, D, E, F, G]).
+
+main:-
+ setof(S, sums_in_squares_fd([1,2,3,4,5,6,7], S), Squares),
+ print_solutions(Squares).