Truncatable primes: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: made REXX code compliant, added DO-END comment labels, removed two blank lines, added other whitespace. -- ~~~~
Line 1,329: Line 1,329:
=={{header|REXX}}==
=={{header|REXX}}==
<lang REXX>/*REXX pgm finds largest left- & right-truncatable primes < 1 million*/
<lang REXX>
/*find largest left- & right-truncatable primes < 1 million.*/
x.=0 /*placeholders for primes. */
x.=0 /*placeholders for primes. */
p.=999 /*default value for P.n */
p.=999 /*default value for P.n */
Line 1,347: Line 1,346:
/*the above 4 lines saves time*/
/*the above 4 lines saves time*/
do k=6 /*divide by known odd primes. */
do k=6 /*divide by known odd primes. */
if p.k**2>j then leave /*only go up to sqrt(J). */
if p.k**2>j then leave /*only go up to sqrt(J). */
if j//p.k==0 then iterate j /*divisible by X? Not prime. */
if j//p.k==0 then iterate j /*divisible by X? Not prime. */
end
end /*k*/


n=n+1 /*bump number of primes found.*/
n=n+1 /*bump number of primes found.*/
p.n=j /*assign to sparse array. */
p.n=j /*assign to sparse array. */
x.j=1 /*indicate that J is a prime. */
x.j=1 /*indicate that J is a prime. */
end
end /*j*/


say 'The last prime is' p.n "("n 'primes under one million).'
say 'The last prime is' p.n "("n 'primes under one million).'
Line 1,361: Line 1,360:


do j=1 for n /*find left-trunc. primes. */
do j=1 for n /*find left-trunc. primes. */
y=p.j; g=y
y=p.j; g=y
if pos(0,y)\==0 then iterate /*if prime contains a 0, nope.*/
if pos(0,y)\==0 then iterate /*if prime contains a 0, nope.*/


do k=1 for length(y)-1 /*test for prime, skip whole #*/
do k=1 for length(y)-1 /*test for prime, skip whole #*/
g=right(y,k); if \x.g then iterate j
g=right(y,k); if \x.g then iterate j
end
end /*k*/


lp=max(lp,y) /*choose the maximum so far. */
lp=max(lp,y) /*choose the maximum so far. */
end
end /*j*/


rp=0
rp=0


do j=1 for n /*find left-trunc. primes. */
do j=1 for n /*find left-trunc. primes. */
y=p.j; g=y
y=p.j; g=y
if pos(0,y)\==0 then iterate /*if prime contains a 0, nope.*/
if pos( 0,y)\==0 then iterate /*if prime contains a 0, nope.*/


do k=1 for length(y)-1 /*test for prime, skip whole #*/
do k=1 for length(y)-1 /*test for prime, skip whole #*/
g=left(y,k); if \x.g then iterate j
g=left(y,k); if \x.g then iterate j
end
end /*k*/


rp=max(rp,y) /*choose the maximum so far. */
rp=max(rp,y) /*choose the maximum so far. */
end
end /*j*/


say 'The largest left-truncatable prime is' lp '(under one million).'
say 'The largest left-truncatable prime is' lp '(under one million).'
say 'The largest right-truncatable prime is' rp '(under one million).'
say 'The largest right-truncatable prime is' rp '(under one million).'</lang>
'''output'''

</lang>
Output:
<pre>
<pre>
The last prime is 999983 (78498 primes under one million).
The last prime is 999983 (78498 primes under one million).