aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-02 07:08:28 +0000
committerGitHub <noreply@github.com>2021-11-02 07:08:28 +0000
commit01cb885b45ef4711286c6503269b3a5ad13064e0 (patch)
treeaf811e3c2dda9f75541fb688bebda46edc777d1c
parent6fbfcfc3b7af57bb22db0ba84405af7f65421abe (diff)
parentde28a61278504b797143899c84b5a4b568409455 (diff)
downloadperlweeklychallenge-club-01cb885b45ef4711286c6503269b3a5ad13064e0.tar.gz
perlweeklychallenge-club-01cb885b45ef4711286c6503269b3a5ad13064e0.tar.bz2
perlweeklychallenge-club-01cb885b45ef4711286c6503269b3a5ad13064e0.zip
Merge pull request #5141 from PerlBoy1967/branch-for-challenge-137
Task 1 & 2
-rwxr-xr-xchallenge-137/perlboy1967/perl/ch-1.pl30
-rwxr-xr-xchallenge-137/perlboy1967/perl/ch-2.pl82
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-137/perlboy1967/perl/ch-1.pl b/challenge-137/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..97960c7091
--- /dev/null
+++ b/challenge-137/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/bin/perl
+
+=pod
+
+Perl Weekly Challenge - 137
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #1 › Long Year
+Submitted by: Mohammad S Anwar
+
+Write a script to find all the years between 1900 and 2100 which is a Long Year.
+
+ || A year is Long if it has 53 weeks.
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+use Date::Calc qw(Week_of_Year);
+
+my @w53years;
+foreach my $y (1900 .. 2099) {
+ push(@w53years,$y) if Week_of_Year($y,12,31) == 53;
+}
+
+printf "Years: %s\n", join(', ', @w53years);
diff --git a/challenge-137/perlboy1967/perl/ch-2.pl b/challenge-137/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..acfe110199
--- /dev/null
+++ b/challenge-137/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/bin/perl
+
+=pod
+
+Perl Weekly Challenge - 137
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #2 › Lychrel Number
+Submitted by: Mohammad S Anwar
+
+You are given a number, 10 <= $n <= 1000.
+
+Write a script to find out if the given number is Lychrel number.
+To keep the task simple, we impose the following rules:
+
+a. Stop if the number of iterations reached 500.
+b. Stop if you end up with number >= 10_000_000.
+
+According to wikipedia:
+
+ || A Lychrel number is a natural number that cannot form a palindrome through
+ || the iterative process of repeatedly reversing its digits and adding the
+ || resulting numbers.
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+use constant MAX_ITER => 500;
+use constant MAX_VALUE => 10_000_000;
+
+use Test::More;
+
+# Prototype(s)
+sub isLychrelNumber($$$;$);
+
+
+my $N = shift // 89;
+
+isLychrelNumber($N,MAX_ITER,MAX_VALUE,1);
+
+my %tests = qw(56 0 57 0 59 0 89 -1);
+foreach my $n (keys %tests) {
+ is(isLychrelNumber($n,MAX_ITER,MAX_VALUE),$tests{$n});
+}
+done_testing();
+
+
+sub isLychrelNumber($$$;$) {
+ my ($n, $maxIter, $maxVal, $print) = @_;
+ $print //= 0;
+
+ my $return = -1;
+
+ while (1) {
+ my $nRev = reverse $n;
+ print "$n + $nRev = " if $print;
+ $n += $nRev;
+ print "$n\n" if $print;
+
+ # Max value exceeded?
+ last if ($n > $maxVal);
+
+ # Palindrome (using a recursive regular expression)?
+ if ($n =~ /^((.)(?1)\2|.?)$/) {
+ $return = 0;
+ last;
+ }
+
+ # Max iterations exceeded?
+ $maxIter--;
+ last if ($maxIter < 0);
+ }
+
+ printf "%d => %d\n", $n, $return if $print;
+
+ $return;
+}