Talk:Rare numbers

From Rosetta Code

comments concerning interesting observations from an webpage

(The author's webpage, the last URL reference from this task's preamble, re-shown below:)


(a URL reference):

  •   author's  website:   rare numbers   by Shyam Sunder Gupta.     (lots of hints and some observations).


I was considering adding checks   (to the REXX program)   to assert that for:

  •   when the number of digits in a rare number is even,   the   sum   must be divisible by   11,     and
  •   when the number of digits in a rare number is   odd,   the   difference   must be divisible by   9.

In fact, all the other (previous) checks   (in the REXX program)   have already filtered out the two (above) wrong cases,   so the above two   interesting observations   are never observed to be false,   so the checks are (at that point) in fact, redundant.

In fact, the webpage section contains a errors,   the difference   must be divisible by   9,   not   11   as it states in the text.

Also, the mention of   A2   and   B2   having to be divisible by some number seems to be also wrong.   I'm attempting to contact the author via e-mail.     -- Gerard Schildberger (talk) 20:59, 8 September 2019 (UTC)


the 1st REXX version

This is the 1st REXX version,   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.*/

  1. = 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.