From 685e4a02f5116bbb45eaf9697a00bb9e0226e85d Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 1 Nov 2021 10:57:26 -0500 Subject: Solve Task #1 Long Year --- challenge-137/bob-lied/README | 4 ++-- challenge-137/bob-lied/perl/ch-1.pl | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 challenge-137/bob-lied/perl/ch-1.pl diff --git a/challenge-137/bob-lied/README b/challenge-137/bob-lied/README index bbaa2cbbcb..1383134251 100644 --- a/challenge-137/bob-lied/README +++ b/challenge-137/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 116 by Bob Lied. +Solutions to weekly challenge 137 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-116/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/ diff --git a/challenge-137/bob-lied/perl/ch-1.pl b/challenge-137/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..b49897d3ef --- /dev/null +++ b/challenge-137/bob-lied/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 137, Task 1: Long Year +# +# 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. +# +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use DateTime; + +my @longYearList; + +for my $year ( 1900 .. 2100 ) +{ + my $dt = DateTime->new(year => $year, month => 12, day => 31); + my ($year, $weeknum) = $dt->week(); + + push @longYearList, $year if $weeknum == 53; +} + +# Output in comma-separated groups of five. +my @row; +while ( @longYearList ) +{ + push @row, join(", ", splice(@longYearList, 0, 5) ); +} +say join(",\n", @row); -- cgit From d1b5c227152dc307c2f07bed723e12acfd9fe71c Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 1 Nov 2021 12:23:05 -0500 Subject: Solve W137 Task 2. --- challenge-137/bob-lied/perl/ch-2.pl | 89 +++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 challenge-137/bob-lied/perl/ch-2.pl diff --git a/challenge-137/bob-lied/perl/ch-2.pl b/challenge-137/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..448f54df01 --- /dev/null +++ b/challenge-137/bob-lied/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge week 137, Task #2, Lychrel number +# +# 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. +# +# Example 1: Input $n = 56, Output = 0 +# 56+65 = 121, found palindrome after 1 iteration +# +# Example 2: Input $n = 57, Output = 0 +# 57+75 = 132 +# 132+231 = 363, found palindrome after 2 iterations +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +my $MaxIter = 500; +my $MaxSum = 10_000_000; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub isPalindrome($n) +{ + return 1 if $n < 10; + my @dig = split("", $n); + while ( @dig && $dig[0] == $dig[-1] ) + { + @dig = @dig[ 1 .. $#dig-1 ]; + } + return @dig == 0; +} + +sub isLychrel($n) +{ + my $iter = 0; + my $sum = $n; + while ( $iter < $MaxIter && $sum < $MaxSum ) + { + my $revn = join("", reverse split(//, $n)); + $sum = $n + $revn; + say "$iter: $n + $revn = $sum" if $Verbose; + return 0 if isPalindrome($sum); + $n = $sum; + } + return 1; +} + +sub runTest +{ + use Test::More; + + is( isPalindrome( 1), 1, "P=1"); + is( isPalindrome( 22), 1, "P=22"); + is( isPalindrome( 27), '', "P=27"); + is( isPalindrome( 343), 1, "P=343"); + is( isPalindrome( 346), '', "P=346"); + + is( isLychrel( 56), 0, "N=56"); + is( isLychrel( 57), 0, "N=57"); + is( isLychrel( 59), 0, "N=59"); + is( isLychrel(196), 1, "N=196"); + + done_testing; +} + -- cgit