diff options
| -rw-r--r-- | challenge-136/james-smith/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-136/james-smith/perl/ch-2.pl | 39 |
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; +} + |
