aboutsummaryrefslogtreecommitdiff
path: root/challenge-136
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-10-31 15:04:38 -0400
committerAdam Russell <ac.russell@live.com>2021-10-31 15:04:38 -0400
commiteedb42ffdc35136187877664edb08f92a310d5fb (patch)
treed0f5794a5a3518126f824463a5c4422111a9a307 /challenge-136
parent9af4627df80018091f1bbf86c07ede1eeeb6548f (diff)
downloadperlweeklychallenge-club-eedb42ffdc35136187877664edb08f92a310d5fb.tar.gz
perlweeklychallenge-club-eedb42ffdc35136187877664edb08f92a310d5fb.tar.bz2
perlweeklychallenge-club-eedb42ffdc35136187877664edb08f92a310d5fb.zip
initial commit
Diffstat (limited to 'challenge-136')
-rw-r--r--challenge-136/adam-russell/blog.txt1
-rw-r--r--challenge-136/adam-russell/blog1.txt1
-rw-r--r--challenge-136/adam-russell/perl/ch-1.pl26
-rw-r--r--challenge-136/adam-russell/perl/ch-2.pl46
-rw-r--r--challenge-136/adam-russell/prolog/ch-1.p18
-rw-r--r--challenge-136/adam-russell/prolog/ch-2.p26
6 files changed, 118 insertions, 0 deletions
diff --git a/challenge-136/adam-russell/blog.txt b/challenge-136/adam-russell/blog.txt
new file mode 100644
index 0000000000..67fbe4a2c1
--- /dev/null
+++ b/challenge-136/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/31/perl
diff --git a/challenge-136/adam-russell/blog1.txt b/challenge-136/adam-russell/blog1.txt
new file mode 100644
index 0000000000..690935d7a9
--- /dev/null
+++ b/challenge-136/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/31/prolog
diff --git a/challenge-136/adam-russell/perl/ch-1.pl b/challenge-136/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..e42a6e283c
--- /dev/null
+++ b/challenge-136/adam-russell/perl/ch-1.pl
@@ -0,0 +1,26 @@
+use strict;
+use warnings;
+##
+# You are given 2 positive numbers, $m and $n.
+# Write a script to find out if the given two numbers are Two Friendly.
+##
+use POSIX;
+use boolean;
+
+sub euclid {
+ my($a, $b) = @_;
+ return ($b) ? euclid($b, $a % $b) : $a;
+}
+
+sub two_friendly{
+ my($m, $n) = @_;
+ my $gcd = euclid($m, $n);
+ my $p = log($gcd) / log(2);
+ return boolean(ceil($p) == floor($p));
+}
+
+MAIN:{
+ print two_friendly(8, 24). "\n";
+ print two_friendly(26, 39). "\n";
+ print two_friendly(4, 10). "\n";
+} \ No newline at end of file
diff --git a/challenge-136/adam-russell/perl/ch-2.pl b/challenge-136/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..fa26451418
--- /dev/null
+++ b/challenge-136/adam-russell/perl/ch-2.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+##
+# You are given a positive number $n.
+# Write a script to find how many different
+# sequences you can create using Fibonacci
+# numbers where the sum of unique numbers in
+# each sequence are the same as the given number.
+##
+use Data::PowerSet q/powerset/;
+
+sub fibonacci_below_n{
+ my($n, $fibonaccis) = @_;
+ $fibonaccis = [1, 1] if !$fibonaccis;
+ my $f = $fibonaccis->[@{$fibonaccis} - 2] + $fibonaccis->[@{$fibonaccis} - 1];
+ if($f < $n){
+ push @{$fibonaccis}, $f;
+ fibonacci_below_n($n, $fibonaccis);
+ }
+ else{
+ shift @{$fibonaccis};
+ return $fibonaccis;
+ }
+}
+
+sub fibonacci_sum{
+ my($n) = @_;
+ my $powerset = powerset(fibonacci_below_n($n));
+ my @summands = grep {
+ my $fibonaccis = $_;
+ my $sum = 0;
+ map{
+ $sum += $_;
+ } @{$fibonaccis};
+ $sum == $n;
+ } @{$powerset};
+ return @summands;
+}
+
+MAIN:{
+ for my $summands (fibonacci_sum($ARGV[0])){
+ print "(" . join(" + ", @{$summands}) . ") = " . $ARGV[0] . "\n";
+ }
+}
+
+
diff --git a/challenge-136/adam-russell/prolog/ch-1.p b/challenge-136/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..ce03b05294
--- /dev/null
+++ b/challenge-136/adam-russell/prolog/ch-1.p
@@ -0,0 +1,18 @@
+:-initialization(main).
+
+two_friendly(M, N):-
+ current_prolog_flag(max_integer, MAX_INTEGER),
+ between(1, MAX_INTEGER, M),
+ between(1, MAX_INTEGER, N),
+ GCD is gcd(M, N),
+ P is log(2, GCD),
+ P0 is ceiling(P),
+ P1 is floor(P),
+ P0 == P1.
+
+main:-
+ (two_friendly(8, 24), format("1~n", _); format("0~n", _)),
+ (two_friendly(26, 39), format("1~n", _); format("0~n", _)),
+ (two_friendly(4, 10), format("1~n", _); format("0~n", _)),
+ halt.
+ \ No newline at end of file
diff --git a/challenge-136/adam-russell/prolog/ch-2.p b/challenge-136/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..78d746a859
--- /dev/null
+++ b/challenge-136/adam-russell/prolog/ch-2.p
@@ -0,0 +1,26 @@
+fibonaccis_below_n(N, Fibonaccis):-
+ fibonaccis_below_n(N, Fibonaccis, 0, [1, 1]).
+fibonaccis_below_n(-1, Fibonaccis, _, Fibonaccis):- !.
+fibonaccis_below_n(N, Fibonaccis, Sum, PartialFibonaccis):-
+ [H0, H1 | _] = PartialFibonaccis,
+ F is H0 + H1,
+ F < N,
+ fibonaccis_below_n(N, Fibonaccis, Sum, [F|PartialFibonaccis]).
+fibonaccis_below_n(N, Fibonaccis, Sum, PartialFibonaccis):-
+ [H0, H1 | _] = PartialFibonaccis,
+ F is H0 + H1,
+ F > N,
+ fibonaccis_below_n(-1, Fibonaccis, Sum, PartialFibonaccis).
+
+sum_x([], 0).
+sum_x([X|Xs], X+Xse):-
+ sum_x(Xs, Xse).
+
+sum_lists(X, N, Xsub):-
+ sublist(Xsub, X),
+ sum_x(Xsub, Xe),
+ N #= Xe.
+
+fibonacci_sum_clp(N, Summands):-
+ fibonaccis_below_n(N, Fibonaccis),
+ sum_lists(Fibonaccis, N, Summands).