diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-03 06:54:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-03 06:54:51 +0000 |
| commit | 727e1da380fdd942f5bce07f71b79bd75ed0ec5b (patch) | |
| tree | c33201e713416ad4170253f3dd8388844794f831 | |
| parent | 7ad7ec75fccaf001ab7453cf05ed1275ea0bc977 (diff) | |
| parent | 9f95ff2232f8433ed009303e8d4ebb21323199d6 (diff) | |
| download | perlweeklychallenge-club-727e1da380fdd942f5bce07f71b79bd75ed0ec5b.tar.gz perlweeklychallenge-club-727e1da380fdd942f5bce07f71b79bd75ed0ec5b.tar.bz2 perlweeklychallenge-club-727e1da380fdd942f5bce07f71b79bd75ed0ec5b.zip | |
Merge pull request #5154 from wlmb/challenges
Add solution to pwc137
| -rw-r--r-- | challenge-137/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-137/wlmb/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-137/wlmb/perl/ch-2.pl | 27 |
3 files changed, 46 insertions, 0 deletions
diff --git a/challenge-137/wlmb/blog.txt b/challenge-137/wlmb/blog.txt new file mode 100644 index 0000000000..3753e2a4c6 --- /dev/null +++ b/challenge-137/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2021/11/02/PWC137/ diff --git a/challenge-137/wlmb/perl/ch-1.pl b/challenge-137/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..8c7175f66f --- /dev/null +++ b/challenge-137/wlmb/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 137 +# Task 1: Long year +# +# See https://wlmb.github.io/2021/11/02/PWC137/#task-1-long-year +use v5.12; +use warnings; +use Text::Wrap qw(wrap $columns); +use integer; +$columns=62; +say wrap("", "", grep {is_long_year($_)} ($ARGV[0]//1900..$ARGV[1]//2100)); +sub is_long_year { + my $YY=shift; + # Skip a day for every 'Gregorian' leap year extrapolating since 0000. + my $first_weekday=(($YY-1)+($YY-1)/4-($YY-1)/100+($YY-1)/400)%7; # 0=Monday + my $leap=$YY%400==0||$YY%4==0&&$YY%100!=0; + return $first_weekday==3 || $leap&&$first_weekday==2; +} diff --git a/challenge-137/wlmb/perl/ch-2.pl b/challenge-137/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..c8cb49bbd5 --- /dev/null +++ b/challenge-137/wlmb/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 137 +# Task 2: Lychrel number +# +# See https://wlmb.github.io/2021/11/02/PWC137/#task-2-lychrel-number +use v5.12; +use warnings; +my ($lower, $upper, $maxcount, $largest)=(10,1000,500,10_000_000); #bounds +foreach(@ARGV){ + my @why=lychrel($_); + say "Input: $_\nOutput: ", @why?"0\n":"1?", @why>1?join("\n", @why):""; + say ""; +} +sub lychrel { + my $n=shift; + my $r=reverse $n; + say("Failed $lower<=$_<=$upper"), exit unless $lower<=$_<=$upper; + my $count=$maxcount; + my @why=("as"); + while(--$count){ + return (@why) if $n eq $r; + push @why, "$n+$r=". ($n+=$r); + return if $n>=$largest; + $r=reverse $n; + } + return; +} |
