I want to move on to solve problem 78, but it is another dynamic programming problem, and therefore I would really like a language in which arrays are pretty easy to use. Admittedly, I haven't actually looked much at R, but it feels like a language that should have pretty good support for arrays, seeing as people use it for processing large data sets and stuff. So, the language I used for problem 45 is a language called Rexx (note: do not make the mistake I made and append an extra x to the end of the name while searching for it!) According to
http://langpop.com/, it is so popular that it just barely edges out brainfuck. Anyway, here is my wonderful solution, it is little more than a transcription of my old R code to Rexx, but it notably includes yet another implementation of the babylonian method for sqrt: maybe after the 20th time I will finally stop needing to consult wikipedia to remember how to do that.
Solution runs in 1.7s on my computer:
numeric digits 20
i = 144
do forever
h = hex(i)
if isPent(h) then
do
say h
leave
end
i = i + 1
end
isPent:
parse arg p
a = sqrt(24*p+1)
return (a*a = 24*p+1) & ((a+1) // 6 = 0)
hex:
parse arg n
return n*(2*n-1)
sqrt:
parse arg N
x1 = N % 2
x2 = N
do while (x2 - x1) > 1
x2 = x1
x1 = (x1 + (N % x1) ) % 2
end
return x2
No comments:
Post a Comment