diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-11-01 17:27:25 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-11-01 17:27:25 +0000 |
| commit | 2d8675b32c102417490b9e3aacdda9ff342da10d (patch) | |
| tree | c50b35db65e412ebf6d8995f7e6395540ad32270 | |
| parent | 10872e8ede889bad86cc0bc71a755cf110463bcd (diff) | |
| download | perlweeklychallenge-club-2d8675b32c102417490b9e3aacdda9ff342da10d.tar.gz perlweeklychallenge-club-2d8675b32c102417490b9e3aacdda9ff342da10d.tar.bz2 perlweeklychallenge-club-2d8675b32c102417490b9e3aacdda9ff342da10d.zip | |
added solutions to 137
| -rw-r--r-- | challenge-137/james-smith/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-137/james-smith/perl/ch-2.pl | 54 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-137/james-smith/perl/ch-1.pl b/challenge-137/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..4ab92a0091 --- /dev/null +++ b/challenge-137/james-smith/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/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 $start_day = 1; ## Jan 1st 1900 was a Monday. + +foreach my $year ( 1900 .. 2100 ) { + my $is_leap_year = ! $year % 4 && ( $year % 100 || ! $year % 400 ); + say $year if $start_day == 4 || $start_day == 3 && $is_leap_year; + $start_day = ( $start_day + 1 + $is_leap_year ) % 7; +} + +## Compute if leap year? +## Long year if starts on Thursday (day 4) or Wednesday (day 3) in a leap year +## Next year starts on the next day of the week Mon->Tues .... +## except in a leap year when it moves 2 Mon->Wed diff --git a/challenge-137/james-smith/perl/ch-2.pl b/challenge-137/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..8951d7c972 --- /dev/null +++ b/challenge-137/james-smith/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/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 $MAX = 1e9; +my $MAX_LARGE = 40; +my $COUNT = 500; +my @TESTS = ( + [ 56, 0 ], + [ 57, 0 ], + [ 59, 0 ], + [196, 1 ], +); + +is( lychrel( $_->[0]), $_->[1] ) foreach @TESTS; +is( lychrel_large($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub lychrel { + my($n,$c) = (shift,$COUNT); + ($n eq reverse $n) ? (return 0) : ($n+= reverse $n) while $c-- && $n <= $MAX; + 1; +} + +sub lychrel_large { + my($n,$c) = (shift,$COUNT); + my @n = split //, $n; + while( $c-- && @n <= $MAX_LARGE ) { + my @r = reverse @n; + return 0 if (join '', @r) eq (join '', @n); + foreach ( reverse 0 .. @n-1 ) { + $n[$_] += $r[$_]; + next if $n[$_] < 10; + $n[$_]-=10; + $_ ? ($n[$_-1]++) : (unshift @n,1); + } + } + 1; +} + + +print "Simple:"; +print " $_" foreach grep { lychrel $_ } 10..1000; +print "\nLarge: "; +print " $_" foreach grep { lychrel_large $_ } 10..1000; +print "\n\n"; + |
