Cuban primes: Difference between revisions

→‎{{header|REXX}}: added additional text to the REXX section header, dropped primality test, used factorization by 6K + 1.
m (added another reference (;Also see).)
(→‎{{header|REXX}}: added additional text to the REXX section header, dropped primality test, used factorization by 6K + 1.)
Line 1,406:
 
=={{header|REXX}}==
Cuban primes can't end in an even (decimal) digit,   or the digit   '''5'''.
 
Also, by their construction, cuban primes can't have a
factor of   '''6*k + 1''',   where   '''k'''   is any positive integer.
<lang rexx>/*REXX program finds and displays a number of cuban primes or the Nth cuban prime. */
numeric digits 20 /*ensure enough decimal digits for #s. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Nth= N<0; N= abs(N) /*used for finding the Nth cuban prime.*/
@.=0; @.0=1; @.2=1; @.3=1; @.4=1; @.5=1; @.6=1; @.8=1 /*ending digs that aren't cubans.*/
#= 0 /*number of cuban primes found so far. */
!.sw=0; linesize() - !.0=1; !.2=1;if !.3=sw<1; then !.4sw=1; 79 !.5=1; /*obtain !.6=1;width of the terminal !screen.8=1; s=41; $=*/
sw $= linesize()7; -#= 1; if sw<1 then sw=79 /*obtainstart widthwith offirst thecuban terminalprime; screen. count.*/
if (Nth & N==1) | N>0 thendo $j=1 7 until #=>N; x= (j+1)**3 - j**3 /*compute a possible cuban prime. /*handle case of the 1st cuban prime.*/
parse var x '' -1 _; if x// k ==0@._ then iterate j /*testcheck forlast adigit "lower"for possiblenon─cuban prime. */
if (Nth & N==2) | N>1 then $= $ 19 /* " " " " 2nd " " */
if (Nth & N==3) | N>2 then $= $ 37 do k=1 until km*km>x; km= k*6 + 1 /*cuban primes " " " " 3rd can't be " ÷ "by 6k+1 */
#= 3 if x//km==0 then iterate j /*Divisible? Then not a /*above are needed forcuban prime. generation*/
N= abs(N) end /*use absolute value of N from now onk*/
if #<N= #+1 then /*bump [↓] the endingnumber digsof thatcuban aren'tprimes cubans.found*/
doif j=4Nth untilthen #=>Ndo; if #==N then do; say commas(x= (j+1)**3; - leave j**3 ; end /*computedisplay a possible1 cubannum.*/
parse var x '' -1 _; if !._ then iterate else iterate /*checkj*/ for the last digit /*keep searching*/
if x// 3==0 then iterate; if x// 7==0 then iterateend /* " " ÷ 3; for ÷ 7 /* [↑] try to fit as many #s per line.*/
ifcx= commas(x//11==0); then iterate; new= if$ x//13==0 cx then iterate /* " " ÷11; "/*insert commas──►X; concat ÷to 13new list*/
if x//17==0length(new)>sw then iteratedo; say if$; x//19= $=0 then iterate cx /*line is " " ÷17; too "long, display ÷#'s 19so far.*/
if x//23==0 then iterate; if x//29==0 then iterate /* " " ÷23; "end /* [↑] initialize the ÷(new) 29next line.*/
if x//31==0 then iterate; if x//37==0 then iterate /* " else " ÷31; " ÷ 37 $= new /*start with cuban # that wouldn't fit.*/
 
do k=s by 6 until k*k>x /*skip multiples of 3 (when using BY 6)*/
if x// k ==0 then iterate j /*test for a "lower" possible prime. */
if x//(k+2) ==0 then iterate j /* " " " "higher" " " */
end /*k*/
#= # + 1 /*bump the number of cuban primes found*/
if Nth then do; if #==N then do; say commas(x); leave j; end /*display 1 num*/
else iterate j /*keep searching. */
end
new=$ commas(x) /*append a new cuban. */
if length(new)>sw then do; say $ /*line too long, show #s*/
$= commas(x) /*initialize next line. */
end
else $= new /*use this longer output*/
end /*j*/
if S\=='' then say $ if \Nth & $\=='' then say $ /*check for any residual cuban primes in $.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/