aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-10-28 22:36:31 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-10-28 22:36:31 +0100
commitaddc01ca111c94e52e6c71212e3fb6f22cfef9a6 (patch)
tree3474fa8e7fe504102562beeaa0ba17d7d1b46f85
parentdc82718186854e495ed16900b1c485d170042a18 (diff)
downloadperlweeklychallenge-club-addc01ca111c94e52e6c71212e3fb6f22cfef9a6.tar.gz
perlweeklychallenge-club-addc01ca111c94e52e6c71212e3fb6f22cfef9a6.tar.bz2
perlweeklychallenge-club-addc01ca111c94e52e6c71212e3fb6f22cfef9a6.zip
solns # 135
-rw-r--r--challenge-136/james-smith/perl/ch-1.pl27
-rw-r--r--challenge-136/james-smith/perl/ch-2.pl39
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-136/james-smith/perl/ch-1.pl b/challenge-136/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..d38d98b87c
--- /dev/null
+++ b/challenge-136/james-smith/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @TESTS = (
+ [ [8,24], 1 ],
+ [ [26,39], 0 ],
+ [ [4,10], 1 ],
+);
+
+is( friendly(@{$_->[0]}), $_->[1] ) foreach @TESTS;
+
+done_testing();
+
+sub friendly {
+ my($a,$b) = @_;
+ ($a,$b) = ($b,$a%$b) while $b; ## Get GCD
+ $a>>=1 until $a&1; ## Remove trailing binary digits
+ return $a == 1 ? 1 : 0; ## For powers of two $a == 1
+}
+
diff --git a/challenge-136/james-smith/perl/ch-2.pl b/challenge-136/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..410c95d2ba
--- /dev/null
+++ b/challenge-136/james-smith/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @TESTS = (
+ [ 16, 4 ],
+ [ 9, 2 ],
+ [ 15, 2 ],
+ [ 99_999, 192 ],
+);
+my @fib = (1,2);
+
+is( fib_sum($_->[0]), $_->[1] ) foreach @TESTS;
+
+done_testing();
+
+
+sub fib_sum {
+ my $n = shift;
+ push @fib, $fib[-1]+$fib[-2] while $n > $fib[-1];
+ return sum( $n, @fib[0..@fib-2] );
+}
+
+sub sum {
+ local $_;
+ my ( $t, @n) = @_;
+ return 1 unless $t;
+ return 0 if $t < 0;
+ my $c = 0;
+ $c += sum( $t-$_, @n ) while $_ = shift @n;
+ return $c;
+}
+