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). */ |
||
⚫ | |||
$=; 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.*/ |
||
⚫ | |||
end /*j*/ |
end /*j*/ |
||
say |
say |
||
w= length(word($, #) ) /*biggest prime length.*/ |
|||
say center(n " Pierpont primes of the " @ |
say center(n " Pierpont primes of the " @.t ' kind', max(10 *(w+1), 80), "═") |
||
⚫ | |||
do p=1 by 10 to #; _=; do k=p for 10; _= _ right( word($, k), w) |
|||
⚫ | |||
end /*k*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
isPrime: procedure; parse arg x '' -1 _; if x<17 then return wordpos(x,"2 3 5 7 11 13")>0 |
|||
if _==5 then return 0; if x//2==0 then return 0 /*not prime. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if x//j==0 then return 0; if x//(j+2)==0 then return 0 /*not 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.*/ |
|||
⚫ | |||
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) |