Talk:Rare numbers: Difference between revisions

→‎the 1<sup>st</sup> REXX version: added a new talk section.
(→‎the 1<sup>st</sup> REXX version: added a new talk section.)
Line 17:
 
Also, the mention of &nbsp; '''A<sup>2</sup>''' &nbsp; and &nbsp; '''B<sup>2</sup>''' &nbsp; having to be divisible by some number seems to be also wrong. &nbsp; I'm attempting to contact the author via e-mail. &nbsp; &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 20:59, 8 September 2019 (UTC)
 
 
== the 1<sup>st</sup> REXX version ==
This is the 1<sup>st</sup> REXX version, &nbsp; before all the optimizations were added:
<lang rexx>/*REXX program to calculate and display an specified amount of rare numbers. */
numeric digits 20; w= digits() + digits() % 3 /*ensure enough decimal digs for calcs.*/
parse arg many start . /*obtain optional argument from the CL.*/
if many=='' | many=="," then many= 3 /*Not specified? Then use the default.*/
#= 0 /*the number of rare numbers (so far)*/
do n=10 /*N=10, 'cause 1 dig #s are palindromic*/
r= reverse(n) /*obtain the reverse of the number N. */
if r>n then iterate /*Difference will be negative? Skip it*/
if n==r then iterate /*Palindromic? Then it can't be rare.*/
s= n+r /*obtain the sum of N and R. */
d= n-r /* " " difference " " " " */
if iSqrt(s)**2 \== s then iterate /*Not a perfect square? Then skip it. */
if iSqrt(d)**2 \== d then iterate /* " " " " " " " */
#= # + 1 /*bump the counter of rare numbers. */
say right( th(#), length(#) + 9) ' rare number is: ' right( commas(n), w)
if #>=many then leave /* [↑] W: the width of # with commas.*/
end /*n*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _
th: parse arg th;return th||word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: parse arg x; $= 0; q= 1; do while q<=x; q= q*4
end /*while q<=x*/
do while q>1; q= q % 4; _= x-$-q; $= $ % 2
if _>=0 then do; x= _; $= $ + q
end
end /*while q>1*/; return $</lang>
Pretty simple, but slow as molasses in January.
 
Not ready for prime time.