aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-26 04:27:57 +0100
committerGitHub <noreply@github.com>2021-10-26 04:27:57 +0100
commit163a9129193a41b9348c61e453f9f938149a2d7d (patch)
tree550c062aeacaec3eb22408a9a804169dbcab6791
parent7f7429586b08b0002b7a80cdc86585b2e2416c11 (diff)
parentc0506fecce98e0797bcbf7e87bd46e9b43a8b2ef (diff)
downloadperlweeklychallenge-club-163a9129193a41b9348c61e453f9f938149a2d7d.tar.gz
perlweeklychallenge-club-163a9129193a41b9348c61e453f9f938149a2d7d.tar.bz2
perlweeklychallenge-club-163a9129193a41b9348c61e453f9f938149a2d7d.zip
Merge pull request #5104 from wlmb/challenges
Solve PWC136
-rw-r--r--challenge-136/wlmb/blog.txt1
-rwxr-xr-xchallenge-136/wlmb/perl/ch-1.pl18
-rwxr-xr-xchallenge-136/wlmb/perl/ch-2.pl24
3 files changed, 43 insertions, 0 deletions
diff --git a/challenge-136/wlmb/blog.txt b/challenge-136/wlmb/blog.txt
new file mode 100644
index 0000000000..dc282c1323
--- /dev/null
+++ b/challenge-136/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/10/25/PWC136/
diff --git a/challenge-136/wlmb/perl/ch-1.pl b/challenge-136/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..6b8a006a73
--- /dev/null
+++ b/challenge-136/wlmb/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 136
+# Task 1: Two friendly
+#
+# See https://wlmb.github.io/2021/10/25/PWC136/#task-1-two-friendly
+use v5.12;
+while(defined(my $x=shift @ARGV) and defined(my $y=shift @ARGV)){
+ my $d=gcd($x, $y);
+ my $b=sprintf "%b", $d;
+ my $output=$b=~/^1(0+)$/||0;
+ my $power=length($1);
+ say "Inputs: $x, $y\nOutput: $output\nSince gcd($x,$y)=$d",
+ $power?"=2**$power":"";
+}
+sub gcd {
+ my ($x,$y)=@_;
+ $y==0?$x:gcd($y,$x%$y);
+}
diff --git a/challenge-136/wlmb/perl/ch-2.pl b/challenge-136/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..4996acdd52
--- /dev/null
+++ b/challenge-136/wlmb/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 136
+# Task 1: Fibonacci sequence
+#
+# See https://wlmb.github.io/2021/10/25/PWC136/#task-2-fibonacci-sequence
+use v5.12;
+foreach(@ARGV){
+ my $count=my @sequences=fib($_,1,1);
+ say "Input: $_\nOutput: $count",
+ $count?join "\n =", "\nAs $_", map {join "+", @$_} @sequences
+ :"";
+}
+
+sub fib {
+ my ($number, $current, $previous)=@_;
+ return
+ $current>$number? ()
+ :$current==$number? ([$current])
+ :(
+ fib($number,$current+$previous, $current),
+ map {unshift @$_, $current; $_}
+ (fib($number-$current,$current+$previous, $current))
+ );
+}