# RogerBW's Blog

 Perl Weekly Challenge 137: Long Cheryl 05 November 2021 I’ve been doing the Weekly Challenges. The latest involved date calculations and numeric palindromes. (Note that this is open until 7 November 2021.) 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. The first assumption I make is that we're dealing with ISO week numbering, simply because it's well-defined. And therefore we have a convenient formula, simple enough that I'm going to make an exception to the general principle of "use the date library rather than writing your own routines". ``````sub p(\$y) { return (\$y+floor(\$y/4)-floor(\$y/100)+floor(\$y/400))%7; } sub longyear { return [grep {p(\$_-1)==3 || p(\$_)==4}, (1900..2100)]; } `````` There are obvious optimisations to be made - `p()` is calculated twice for most years, and could reasonably be cached. But apart from some details of how the `grep`-equivalent varies among languages, the others look basically the same as this. I do rather like the symmetry of the PostScript version. ``````/p { dup dup dup 4 idiv 4 1 roll 100 idiv neg 4 1 roll 400 idiv add add add 7 mod } bind def `````` 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. `````` A basic problem here is that because there are no such numbers known (in base 10) it's a little hard to say what one would look like, and therefore where one would end the testing. (For example, many such problems can be solved by looking for repeated values, but adding a number to its own reversal will always produce a number larger than either, so there won't be any loops.) I deal with this by ignoring it. Instead, if I do find a palindrome I return 0; if I exceed either the iteration or size limit I return -1 (which one might regard as meaning "worthy of further investigation"). Here's the Ruby; the others implement the same algorithm with more or less verbosity. ``````def lychrel(nn) n=nn 1.upto(100) do m=n.to_s.reverse.to_i if m==n then return 0 end n+=m if n>1e7 then break end end return -1 end `````` PostScript needs a "clean" integer-to-string routine since `cvs` wants a string length: ``````/i2s { dup log cvi 1 add string cvs } bind def `````` and a string reverser: ``````/reverse { 2 dict begin dup length dup /l exch def string /out exch def { /l l 1 sub def out exch l exch put } forall out end } bind def `````` but then looks quite similar when it comes down to the main calculation. ``````/lychrel { /ret -1 def 500 { dup i2s reverse cvi dup 2 index eq { /ret 0 def pop exit } if add dup 1e7 ge { exit } if } repeat pop ret } bind def `````` Full code on github. Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one. Search 10 per page 20 per page 50 per page 100 per page Archive Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult Special All book reviews, All film reviews
Produced by aikakirja v0.1