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.