Frobenius numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added the computer programming language REXX.)
m (added whitespace.)
Line 5: Line 5:
a(n) = prime(n)*prime(n+1) - prime(n) - prime(n+1), where '''prime(n) < 10,000'''
a(n) = prime(n)*prime(n+1) - prime(n) - prime(n+1), where '''prime(n) < 10,000'''
<br><br>
<br><br>

=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds Frobenius numbers where the numbers are less than some number N. */
<lang rexx>/*REXX program finds Frobenius numbers where the numbers are less than some number N. */

Revision as of 23:29, 1 April 2021

Frobenius 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.
Task

a(n) = prime(n)*prime(n+1) - prime(n) - prime(n+1), where prime(n) < 10,000

REXX

<lang rexx>/*REXX program finds Frobenius numbers where the numbers are less than some number N. */ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 10000 /* " " " " " " */ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 10 /*width of a number in any column. */

                                   @Frob= ' Frobenius numbers that are  < '    commas(hi)

if cols>0 then say ' index │'center(@Frob, 1 + cols*(w+1) ) if cols>0 then say '───────┼'center("" , 1 + cols*(w+1), '─') idx= 1 /*initialize the index of output lines.*/ $= /*a list of Frobenius numbers (so far)*/

    do j=1;    jp= j + 1                        /*generate   Frobenius numbers  <  HI  */
    y= @.j * @.jp   -   @.j  -  @.jp
    if y>= hi  then leave
    if cols==0           then iterate           /*Build the list  (to be shown later)? */
    c= commas(y)                                /*maybe add commas to the number.      */
    $= $ right(c, max(w, length(c) ) )          /*add a Frobenius #──►list, allow big #*/
    if j//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(j-1) @FROB 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: @.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */

                       #=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           /*bump # Ps;  assign next P;  P squared*/
       end          /*j*/;   return</lang>
output   when using the default inputs:
 index │                                     Frobenius numbers that are  <  10,000
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          1          7         23         59        119        191        287        395        615        839
  11   │      1,079      1,439      1,679      1,931      2,391      3,015      3,479      3,959      4,619      5,039
  21   │      5,615      6,395      7,215      8,447      9,599

Found  25  Frobenius numbers that are  <  10,000

Ring

<lang ring> load "stdlib.ring"

decimals(0) see "working..." + nl see "Frobenius numbers are:" + nl

row = 0 limit1 = 101 Frob = []

for n = 1 to limit1

   if isprime(n)
      add(Frob,n)
   ok

next

for n = 1 to len(Frob)-1

   row = row + 1
   fr = Frob[n]*Frob[n+1]-Frob[n]-Frob[n+1]
   see "" + fr + " "
   if row%5 = 0
      see nl
   ok

next

see "Found " + row + " Frobenius primes" + nl see "done..." + nl </lang>

Output:
working...
Frobenius primes are:
1 7 23 59 119 
191 287 395 615 839 
1079 1439 1679 1931 2391 
3015 3479 3959 4619 5039 
5615 6395 7215 8447 9599 
Found 25 Frobenius primes
done...