Pierpont primes: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: centered the output better.)
m (→‎{{header|REXX}}: simplified code, improved for speed.)
Line 1,998: Line 1,998:
numeric digits n /*ensure enough decimal digs (bit int).*/
numeric digits n /*ensure enough decimal digs (bit int).*/
big= copies(9, digits() ) /*BIG: used as a max number (a limit).*/
big= copies(9, digits() ) /*BIG: used as a max number (a limit).*/
@.= '2nd'; @.1= '1st'
@= '1st'
do t=1 to -1 by -2; usum= 0; vsum= 0; s= 0 /*T is 1, then -1.*/
do t=1 to -1 by -2; usum= 0; vsum= 0; s= 0 /*T is 1, then -1.*/
#= 0 /*number of Pierpont primes (so far). */
#= 0 /*number of Pierpont primes (so far). */
w= 0 /*maximum width of the Pierpont primes.*/
$=; do j=0 until #>=n /*$: the list " " " " */
$=; do j=0 until #>=n /*$: the list " " " " */
if usum<=s then usum= get(2, 3); if vsum<=s then vsum= get(3, 2)
if usum<=s then usum= get(2, 3); if vsum<=s then vsum= get(3, 2)
s= min(vsum, usum); if \isPrime(s) then iterate /*get min; Not prime? */
s= min(vsum, usum); if \isPrime(s) then iterate /*get min; Not prime? */
#= # + 1; $= $ s /*bump counter; append.*/
#= # + 1; $= $ s /*bump counter; append.*/
w= max(w, length(s) ) /*find max prime width.*/
end /*j*/
end /*j*/
say
say
if t==-1 then @= '2nd' /*choose word for type.*/
w= length(word($, #) ) /*biggest prime length.*/
say center(n " Pierpont primes of the " @ ' kind', max(10 *(w+1), 80), "═")
say center(n " Pierpont primes of the " @.t ' kind', max(10 *(w+1), 80), "═")

call show $ /*display the primes. */
do p=1 by 10 to #; _=; do k=p for 10; _= _ right( word($, k), w)
end /*t*/
exit /*stick a fork in it, we're all done. */
end /*k*/
if _\=='' then say substr( strip(_, "T"), 2)
end /*p*/
end /*t*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 by 10 to words($); _=
isPrime: procedure; parse arg x '' -1 _; if x<17 then return wordpos(x,"2 3 5 7 11 13")>0
do k=j for 10; _= _ right( word($, k), w)
if _==5 then return 0; if x//2==0 then return 0 /*not prime. */
if x//3==0 then return 0; if x//7==0 then return 0 /* " " */
end /*k*/
do j=11 by 6 until j*j>x /*skip ÷ 3's.*/
if _\=='' then say substr( strip(_, 'T'), 2)
end /*j*/; return
if x//j==0 then return 0; if x//(j+2)==0 then return 0 /*not prime. */
end /*j*/; return 1 /*it's prime.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isPrime: procedure; parse arg x; if x<2 then return 0 /*not prime.*/
if wordpos(x, '2 3 5 7')\==0 then return 1 /*it's prime.*/
if x//2==0 then return 0; if x//3==0 then return 0 /*not prime.*/
do j=5 by 6 until j*j>x
if x//j==0 then return 0; if x//(j+2)==0 then return 0 /*not prime.*/
end /*j*/; return 1 /*it's prime.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
get: parse arg c1,c2; m=big; do ju=0; pu= c1**ju; if pu+t>s then return min(m, pu+t)
get: parse arg c1,c2; m=big; do ju=0; pu= c1**ju; if pu+t>s then return min(m, pu+t)