diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2021-11-02 15:35:18 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2021-11-02 15:35:18 -0400 |
| commit | bd19464cd712868753a52a7b1fc428f87ac0edeb (patch) | |
| tree | 555f6def7f1dbdc7e7dc07645a998327c9840c74 | |
| parent | acb047372c5060d4b5c1337a281a1e25c41c9aea (diff) | |
| download | perlweeklychallenge-club-bd19464cd712868753a52a7b1fc428f87ac0edeb.tar.gz perlweeklychallenge-club-bd19464cd712868753a52a7b1fc428f87ac0edeb.tar.bz2 perlweeklychallenge-club-bd19464cd712868753a52a7b1fc428f87ac0edeb.zip | |
new file: challenge-137/mattneleigh/perl/ch-1.pl
new file: challenge-137/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-137/mattneleigh/perl/ch-1.pl | 99 | ||||
| -rwxr-xr-x | challenge-137/mattneleigh/perl/ch-2.pl | 106 |
2 files changed, 205 insertions, 0 deletions
diff --git a/challenge-137/mattneleigh/perl/ch-1.pl b/challenge-137/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..a5c33edecc --- /dev/null +++ b/challenge-137/mattneleigh/perl/ch-1.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @years = (); + +print("\n"); +for(1900 .. 2100){ + # See if this year is a long year... + if(weeks_in_year($_) == 53){ + # Store it if it is... + push(@years, $_); + if($#years == 4){ + # There are five years stored- print + # them out and flush the year-buffer + print(join(", ", @years), "\n"); + @years = (); + } + } +} +if(@years){ + # Some unprinted years remain; print + # them out too + print(join(", ", @years), "\n"); +} +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine how many weeks there are in a given year +# Takes one argument: +# * The year (e.g. 1937) +# Returns: +# * The number of weeks in the year, which will be either 52 or 53 +################################################################################ +sub weeks_in_year{ + my $year = shift(); + + # Did this year end on Thursday or the + # previous year end on Wednesday? + if( + (new_years_eve_weekday($year) == 4) + || + (new_years_eve_weekday($year - 1) == 3) + ){ + # Yes... + return(53); + } else{ + # No... + return(52); + } + +} + + + +################################################################################ +# Determine on which day of the week December 31 falls in a given year +# Takes one argument: +# * The year (e.g. 1937) +# Returns: +# * The day of the week on which December 31 occurs in the given year, ranging +# from 0 (Sunday) to 6 (Saturday) +################################################################################ +sub new_years_eve_weekday{ + use POSIX; + + my $year = shift(); + + return( + ( + $year + + + floor($year / 4) + - + floor($year / 100) + + + floor($year / 400) + ) + % + 7 + ); + +} + + + diff --git a/challenge-137/mattneleigh/perl/ch-2.pl b/challenge-137/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..9a08119cd2 --- /dev/null +++ b/challenge-137/mattneleigh/perl/ch-2.pl @@ -0,0 +1,106 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @numbers = ( + # Given cases + 56, 57, 59, + + # Additional test cases + 1500, 196 +); +my $number; +my $iterations = 0; + +print("\n"); +foreach $number (@numbers){ + my $rval = is_lychrel_number($number, \$iterations); + + print("Input: \$n = $number\n"); + print("Output: ", defined($rval) ? $rval : "Invalid input", "\n"); + print( + "After $iterations iteration", + ($iterations == 1 ? "" : "s"), + ", we found ", + defined($rval) + ? + ($rval ? "no palindromes" : "a palindrome") + : + "that our input was not valid", + ".\n\n" + ); + $iterations = 0; +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether a number appears to be a Lychrel number- a natural number +# that cannot form a palindrome through the iterative process of repeatedly +# reversing its digits and adding the resulting numbers. To limit the use of +# computing resources, this function will declare an input value to be a +# probable Lychrel number if the running total exceeds 10000000 or more than +# 500 iterations have passed. Input values are also restricted to those +# between 10 and 1000, inclusive. +# Takes two arguments: +# * A number N to evaluate +# * Optionally, a ref to a scalar that may be populated with the number of +# iterations required to determine whether N appeared to be a Lychrel number +# Returns on success: +# * 0 if N does not appear to be a Lychrel number +# * 1 if N appears to be a Lychrel number +# Returns on error: +# * undef if N is less than 10 or more than 1000 +################################################################################ +sub is_lychrel_number{ + my $n = int(shift()); + my $iter = shift(); + + $$iter = 0 if(defined($iter)); + + # Establish a range of valid inputs + return(undef) unless(($n > 9) && ($n < 1001)); + + my $i = 0; + my $reversed = 0; + + while($i <= 500){ + # Swap digits around and compare... + $reversed = reverse(split("", $n)); + if($reversed == $n){ + # Found a palindrome- this is not a + # Lychrel number + $$iter = $i if(defined($iter)); + return(0); + } + + # Add the reversed number to the total, + # and bail if the number is too large + $n += $reversed; + last if($n >= 10000000); + + $i++; + } + + # If we got here, either the total got + # too large, or we ran out of + # iterations; we'll assume this is a + # Lychrel number + $$iter = $i if(defined($iter)); + return(1); + +} + + + |
