Truncatable primes: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added comments & whitespace, support for specifying the upper limit, optimized prime number generator. -- ~~~~) |
m (→{{header|REXX}}: removed two more-or-less useless lines of code. -- ~~~~) |
||
Line 1,590: | Line 1,590: | ||
n=7; s.7=17**2 /*number of primes so far. */ |
n=7; s.7=17**2 /*number of primes so far. */ |
||
/*───────────────────────────────────────generate primes ≤ high. */ |
/*───────────────────────────────────────generate primes ≤ high. */ |
||
call time 'Reset' |
|||
do j=p.n+2 by 2 to high /*only find odd Primes from here.*/ |
do j=p.n+2 by 2 to high /*only find odd Primes from here.*/ |
||
if j//3 ==0 then iterate /*divisible by three? */ |
if j//3 ==0 then iterate /*divisible by three? */ |
||
Line 1,605: | Line 1,604: | ||
!.j=1 /*indicate that J is a prime.*/ |
!.j=1 /*indicate that J is a prime.*/ |
||
end /*j*/ |
end /*j*/ |
||
say 'generation of primes took' format(time('E'),,2) "seconds." |
|||
/*───────────────────────────────────────find largest left truncatable P*/ |
/*───────────────────────────────────────find largest left truncatable P*/ |
||
do L=n by -1 for n while lp==0; if pos(0,p.L)\==0 then iterate |
do L=n by -1 for n while lp==0; if pos(0,p.L)\==0 then iterate |
||
Line 1,611: | Line 1,609: | ||
if \!._ then iterate L /*Truncated # ¬prime? Skip it.*/ |
if \!._ then iterate L /*Truncated # ¬prime? Skip it.*/ |
||
end /*k*/ |
end /*k*/ |
||
leave /*leave the DO loop, we found one*/ |
|||
lp=p.L |
|||
end /*L*/ |
end /*L*/ |
||
/*───────────────────────────────────────find largest right truncatable P*/ |
/*───────────────────────────────────────find largest right truncatable P*/ |
||
Line 1,618: | Line 1,616: | ||
if \!._ then iterate R /*Truncated # ¬prime? Skip it.*/ |
if \!._ then iterate R /*Truncated # ¬prime? Skip it.*/ |
||
end /*k*/ |
end /*k*/ |
||
leave /*leave the DO loop, we found one*/ |
|||
rp=p.R |
|||
end /*R*/ |
end /*R*/ |
||
/*───────────────────────────────────────show largest left/right trunc P*/ |
/*───────────────────────────────────────show largest left/right trunc P*/ |
||
say 'The last prime is' p.n " (there are" n 'primes ≤' high")." |
say 'The last prime is' p.n " (there are" n 'primes ≤' high")." |
||
say copies('─',70) /*show a separator line. */ |
say copies('─',70) /*show a separator line. */ |
||
say 'The largest left-truncatable prime ≤' high " is " |
say 'The largest left-truncatable prime ≤' high " is " p.L |
||
say 'The largest right-truncatable prime ≤' high " is " |
say 'The largest right-truncatable prime ≤' high " is " p.R |
||
/*stick a fork in it, we're done.*/</lang> |
/*stick a fork in it, we're done.*/</lang> |
||
'''output''' |
'''output''' |