* lar3ryca <
d50283ce-ee8c-4fe6...@googlegroups.com> :
Wrote on Mon, 6 Dec 2021 13:26:38 -0800 (PST):
> On Monday, December 6, 2021 at 4:46:37 AM UTC-6, Madhu wrote:
> I never did use REXX on an IBM system. My introduction to it was
> ARexx, on the Amiga. What made is especially useful was that some
> programs had a built-in Arexx interface, allowing an ARexx program to
> do pretty much whatever the programmer wanted to expose to ARexx.
>> You can reduce the brute force problem space by half because the 4 year
>> YYYY fully determines if a 8-character date is a palindrome or not:
>>
>> ;; YYYY-MM-DD: ABCD-DC-BA
>> ;; YYYY-DD-MM: ABCD-DC-BA
>>
>> And you can check if a year is a palindrome or not and you are done
>> without generating all the strings for every day and month.
[snip]
> And quickly my eyes glazed over. I knew there was a reason I never
> learned LISP. I cannot, for the life of me, figure out how you are
> making the determination that the year is a palindrome.
Sorry, I did spring it on you to see if it was readable and the
algorithm was understandable and I guess that answers it. The posted
code has at least one off-by-one bug and is missing 2 lines in one
function lost in cut & paste
> Nice. I can only assume that it's fasfter than mine, which takes about
> 0.033 seconds for 2021 to 2099.
The range is too small for any comparison and i dont think you can
compare script executions with the `time' command meaningfully
I downloaded and installed regina-rexx and read the wikipedia page for
the syntax for 2-3 hours I came up with this: the algorithm should be
faster. maybe you can check or comment
#!/usr/bin/rexx
startyear = 2021
endyear = 2099
do y = startyear to endyear
if value(palindrome_year_p(y)) then
say y||reverse(y)
end
return 0
gregorian_leap_year_p:
parse arg year
p = year // 4
q = year // 400
return (p = 0) & ^ (q = 100 | q = 200 | q = 300)
days_in_month:
parse arg year month
select
when month = 1 | month = 3 | month = 5 | month = 8 | month = 10 | month = 12 then return 31
when month = 4 | month = 6 | month = 9 | month = 11 then return 30
when month = 2 then do
if gregorian_leap_year_p(year) then return 29
else return 28
end
otherwise
say "invalid month" month
exit 1
end
/* ABCD-DC-BA = YYYY-MM-DD*/
palindrome_year_p:
parse arg year
parse var year 1 ab 3 3 cd 5
mm = reverse(ab)
dd = reverse(cd)
return 0 < mm & mm < 13 & 0 < dd & dd < 1 + days_in_month(year mm)