Unprimeable numbers

Revision as of 13:50, 1 December 2019 by rosettacode>Gerard Schildberger (added a draft task and also a REXX programming solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

As used here, all unprimeable numbers   (positive integers)   are always expressed in base ten.

Unprimeable numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definitions


───── Definition from OEIS ─────:
Unprimeable numbers are composite numbers that always remain composite when a single decimal digit of the number is changed.


───── Definition from Wiktionary   (referenced from Adam Spencer's book) ─────:
(arithmetic)   that cannot be turned into a prime number by changing just one of its digits to any other digit..   (sic)


Unprimeable numbers are also spelled:   unprimable.

All one─ and two─digit numbers can be turned into primes by changing a single decimal digit.


Examples

190   isn't unprimeable,   because by changing the zero digit into a three yields   193,   which is a prime.


The number   200   is unprimeable,   since none of the numbers   201, 202, 203, ··· 209   are prime, and all the other numbers obtained by changing a single digit to produce   100, 300, 400, ··· 900,   or   210, 220, 230, ··· 290   which are all even.


It is valid to change   189   into   089   by changing the   1   (one)   into a   0   (zero),   which then the leading zero can be removed,   and then treated as if the   "new"   number is   89.


Task
  •   show the first   25   unprimeable numbers   (horizontally, on one line, preferably with a title)
  •   show the   600th   unprimeable number
  •   (optional) show the lowest unprimeable number ending in a specific decimal digit   (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  •   (optional) use commas in the numbers where appropriate


Show all output here, on this page.


Also see



REXX

<lang rexx>/*REXX program finds and displays unprimeable numbers (non-negative integers). */ parse arg n x hp . /*obtain optional arguments from the CL*/ if n== | n=="," then n= 35 /*Not specified? Then use the default.*/ if x== | x=="," then x= 600 /* " " " " " " */ if hp== | hp=="," then hp= 10000000 /* " " " " " " */ eds=4; ed.1= 1; ed.2= 3; ed.3= 7; ed.4= 9 /*the "end" digits which are prime; #>9*/ call genP hp /*generate primes up to & including HP.*/

  1. = 0 /*number of unprimeable numbers so far.*/

$$=; $.=. /*a list " " " " " */

                                                /*1─ and 2─digit #'s are all primeable.*/
     do j=100;            if !.j  then iterate  /*Prime?  Unprimeable must be composite*/
     L= length(j)                               /*obtain the  length of the number  J. */
     meat= left(j, L-1)                         /*obtain the first  L-1  digits of  J. */
                                                /* [↑]  examine the "end" digit of  J. */
       do e_=1  for eds;  new= meat || ed.e_    /*obtain a different number  (than  J).*/
       if new==j  then iterate                  /*Is it the original number? Then skip.*/
       if !.new   then iterate j                /*This new number not prime?   "    "  */
       end  /*e_*/
     meat= right(j, L-1)                        /*obtain the  last  L-1  digits of  J. */
                                                /* [↑]  examine a new 1st digit of  J. */
       do f_=0  for 10;  new= (f_||meat) + 0    /*obtain a different number  (than  J).*/
       if new==j  then iterate                  /*Is it the original number? Then skip.*/
       if !.new   then iterate j                /*This new number not prime?   "    "  */
       end  /*f_*/                              /* [↑]  examine the front digit of  J. */
                   do a_= 2  to L-1             /*traipse through the middle digits.   */
                   meat=   left(j, a_ - 1)      /*use a number of left─most dec. digits*/
                   rest= substr(j, a_ + 1)      /* "  "    "    " right─most "     "   */
                      do n_=0  for 10           /*traipse through all 1─digit numbers. */
                      new= meat || n_ || rest   /*construct new number, like a phoenix.*/
                      if new==j  then iterate   /*Is it the original number? Then skip.*/
                      if !.new   then iterate j /*This new number not prime?   "    "  */
                      end   /*n_*/
                   end       /*a_*/
     #= # + 1                                   /*bump the count of unprimeable numbers*/
     if #<n     then $$= $$ j                   /*maybe add unprimeable # to  $$  list.*/
     if #==x    then $.ox= j                    /*assign the   Xth  unprimeable number.*/
     _= right(j, 1)                             /*obtain the right─most dec digit of J.*/
     if $._==.  then $._= j                     /*the 1st unprimeable # that ends in _.*/
     if $.3==.  then iterate;  if $.7==.  then iterate    /*test if specific #'s found.*/
     if $.1==.  then iterate;  if $.9==.  then iterate    /*  "   "     "     "    "   */
     leave                                                /*if here,  then we're done. */
     end   /*j*/

if n>0 then do; say center(' first ' n "unprimeable numbers ", 135, '═')

                 say strip($$);    say
            end

if x>0 then say ' the ' th(x) " unprimeable number is: " commas($.ox) say

    do o=0  for 10;      if length($.o)==0  then iterate
    say '     the first unprimeable number that ends in '  o  " is:"right(commas($.o),11)
    end   /*o*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do c=length(?)-3 to 1 by -3; ?=insert(',', ?, c); end; return ? th:procedure;parse arg x;return x||word('th st nd rd',1+(x//10)*(x//100%10\==1)*(x//10<4)) /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6= 13; nP=6 /*assign low primes; # primes. */

     !.=0;   !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /*assign some  low  semaphore primes.  */
      do lim=100  until lim*lim>=hp;  end       /*only keep primes up to the sqrt(hp). */
      do j=@.nP+4  by 2  to hp                  /*only find odd primes from here on.   */
      if j// 3==0  then iterate                 /*is J divisible by 3?  Then not prime.*/
      parse var j  -1 _;if _==5  then iterate /*Is last digit a "5"?    "   "    "   */
      if j// 7==0  then iterate                 /*is J divisible by 7?  Then not prime.*/
      if j//11==0  then iterate                 /*is J divisible by 11? Then not prime.*/
      if j//13==0  then iterate                 /*is J divisible by 13? Then not prime.*/
         do k=7  while k*k<=j                   /*divide by some known low odd primes. */
         if j // @.k==0  then iterate j         /*Is J divisible by P?  Then not prime.*/
         end   /*k*/                            /* [↓]  a prime  (J)  has been found.  */
      nP= nP+1;  if nP<=lim  then @.nP=j; !.j=1 /*bump prime count; assign prime to  @.*/
      end      /*j*/;             return</lang>
output   when using the default inputs:

(Shown at   5/6   size.)

════════════════════════════════════════════════════ first  35 unprimeable numbers ════════════════════════════════════════════════════
200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848

     the  600th  unprimeable number is:  5,242

     the first unprimeable number that ends in  0  is:        200
     the first unprimeable number that ends in  1  is:    595,631
     the first unprimeable number that ends in  2  is:        322
     the first unprimeable number that ends in  3  is:  1,203,623
     the first unprimeable number that ends in  4  is:        204
     the first unprimeable number that ends in  5  is:        325
     the first unprimeable number that ends in  6  is:        206
     the first unprimeable number that ends in  7  is:    872,897
     the first unprimeable number that ends in  8  is:        208
     the first unprimeable number that ends in  9  is:    212,159