Next special primes

From Rosetta Code
Revision as of 08:49, 26 March 2021 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: moved some statements.)
Next special primes 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.
Task

n   is smallest prime such that the difference of successive terms is strictly increasing, where     n   <   1050.

REXX

Translation of: RING

<lang rexx>/*REXX program finds next special primes: difference of successive terms is increasing.*/ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 1050 /* " " " " " " */ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 10 /*width of a number in any column. */

                       @nsp= ' next special primes  < '      commas(hi)   ,
                             " such that the different of successive terms is increasing"

if cols>0 then say ' index │'center(@nsp , 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') op= @.1 /*assign oldPrime to the first prime.*/ nsp= 0; idx= 1 /*initialize number of nsp and index.*/ $= /*a list of nice primes (so far). */

    do j=1;                np= op + j           /*assign newPrime to oldPrime  +  j    */
    if np>=hi         then leave                /*Is  newPrime ≥ hi?  Then leave loop. */
    if \!.np          then iterate              /*Is  np  a prime?   Then skip this  J.*/
    nsp= nsp + 1                                /*bump the number of   nsp's.          */
    op= np                                      /*set oldPrime to the value of newPrime*/
    if cols==0        then iterate              /*Build the list  (to be shown later)? */
    c= commas(np)                               /*maybe add commas to the number.      */
    $= $ right(c, max(w, length(c) ) )          /*add a nice prime ──► list, allow big#*/
    if nsp//cols\==0  then iterate              /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end   /*j*/

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say say 'Found ' commas(nsp) @nsp exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0 /*placeholders for primes (semaphores).*/

     @.1=2;  @.2=3;  @.3=5;  @.4=7;  @.5=11     /*define some low primes.              */
     !.2=1;  !.3=1;  !.5=1;  !.7=1;  !.11=1     /*   "     "   "    "     flags.       */
                       #=5;     s.#= @.# **2    /*number of primes so far;     prime². */
                                                /* [↓]  generate more  primes  ≤  high.*/
       do j=@.#+2  by 2  to hi                  /*find odd primes from here on.        */
       parse var j  -1 _; if     _==5  then iterate  /*J divisible by 5?  (right dig)*/
                            if j// 3==0  then iterate  /*"     "      " 3?             */
                            if j// 7==0  then iterate  /*"     "      " 7?             */
                                                /* [↑]  the above five lines saves time*/
              do k=5  while s.k<=j              /* [↓]  divide by the known odd primes.*/
              if j // @.k == 0  then iterate j  /*Is  J ÷ X?  Then not prime.     ___  */
              end   /*k*/                       /* [↑]  only process numbers  ≤  √ J   */
       #= #+1;    @.#= j;    s.#= j*j;   !.j= 1 /*bump # of Ps; assign next P;  P²; P# */
       end          /*j*/;   return</lang>
output   when using the default inputs:
 index │            next special primes  <  1,050  such that the different of successive terms is increasing
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          3          5         11         19         29         41         59         79        101        127
  11   │        157        191        227        269        313        359        409        461        521        587
  21   │        659        733        809        887        967      1,049

Found  26  next special primes  <  1,050  such that the different of successive terms is increasing

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl

row = 0 num = null limit1 = 100 nextPrime = 2 oldPrime = 2

for n = 1 to limit1

   nextPrime = oldPrime + n
   if isprime(nextPrime)
      row = row + 1
      see "" + nextPrime + " "
      if (row%5) = 0
          see nl
      ok
      oldPrime = nextPrime
   ok

next

see nl + "done..." + nl

</lang>

Output:
working...
3 5 11 19 29 
41 59 79 101 127 
157 191 227 269 313 
359 409 461 521 587 
659 733 809 887 967 
1049 
done...