Weekly Challenge 137
Tags: perl, theweeklychallenge
Published: 2021-11-01 13:17:14

[Challenge](https://theweeklychallenge.org/blog/perl-weekly-challenge-137/), [My solutions](https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-137/sgreen)

So while Mohammad was commiserating the Indians losing to the Black Caps last night, as a Kiwi living in Australia I was a very happy boy. Well done to Kane Williamson and the lads.

## TASK #1 › Long Year

### Task

Write a script to find all the years between 1900 and 2100 which is a Long Year.

### My solution

When we switched to the [Gregorian calendar](https://www.timeanddate.com/calendar/julian-gregorian-switch.html) in 1752, they definitely didn't have computers in their mind. Date math really sucks. If we thought Y2K was serious, I suspect (the non-existent) 29/2/2100 is going to be even worse. We've got to deal with the [2038 date problem](https://en.wikipedia.org/wiki/Year_2038_problem) first. But I digress.

The terms 'long year' and '53 week years' aren't used at all in New Zealand and Australia, so I had to look up the meaning. According to [this article](https://www.timeanddate.com/date/week-numbers.html) "if the year starts on a Thursday or is a leap year that starts on a Wednesday, that particular year will have 53 numbered weeks. These week numbers are commonly used in some European and Asian countries; but not so much in the United States."

This seems rather simple exercise after that. We start with the fact that 1/1/1900 was a Monday. I set `$year` to 1900 and `$day_of_week` to 1 (where 0 = Sunday, 6 = Saturday). If day of week is 4 (Thursday) or 3 (Wednesday) in a leap year, I add it to the `@long_years` array.

The next fact is the first day of the next year is either one day (or two days for leap years) later than the current year. For example, 1/1/1900 was a Monday, so 1/1/1901 was a Tuesday. Remember 1900 was NOT a leap year. I change `$year` and `$day_of_week` as required, and keep running the loop until 2100.

Comparing the output to the example gives the same result, so hopefully it's all good.

### Examples

```
$ ./ch-1.pl 
1903, 1908, 1914, 1920, 1925, 1931, 1936, 1942, 1948, 1953, 1959, 1964, 1970, 1976, 1981, 1987, 1992, 1998, 2004, 2009, 2015, 2020, 2026, 2032, 2037, 2043, 2048, 2054, 2060, 2065, 2071, 2076, 2082, 2088, 2093, 2099
```

## TASK #2 › Lychrel Number

### Task

You are given a number, 10 ≤ $n ≤ 1000.

Write a script to find out if the given number is [Lychrel number](https://en.wikipedia.org/wiki/Lychrel_number). To keep the task simple, we impose the following rules:

 * Stop if the number of iterations reached 500.
 * Stop if you end up with number >= 10,000,000.

### My solution

If I thought date math was hard, number theory blows my mind. The Wikipedia page contains the equation below, which I don't even know where to begin!

 ![Complex equation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m1ppwjck2yw9gy28p47.png)

So it's the brute force method for me. One great thing about Perl is that distinction between a number and a string that contains a number is almost non-existent. I know in the internals it's different, but from a coding point of view the transition between the two is seamless.

I check if the input is palindromic by comparing using the reverse function (which reverses the string in a scalar context). If it isn't I add the two numbers together and repeat the process until it is a match or we have reached the rules above.

### Example

```
$ ./ch-2.pl 56
0

$ ./ch-2.pl 57
0

$ ./ch-2.pl 59
0

$ ./ch-2.pl 788
1
```