Truncatable primes: Difference between revisions
Content added Content deleted
(Added Pike implementation) |
(Rewrite Pike code to be simpler and faster) |
||
Line 2,320: | Line 2,320: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
<lang Pike>bool is_trunc_prime(int p, string direction) |
|||
⚫ | |||
array get_primes(int roof) |
|||
{ |
|||
int p = 2; |
|||
array primes = ({}); |
|||
do { |
|||
primes += ({ p }); |
|||
p = p->next_prime(); |
|||
} while (p < roof); |
|||
return primes; |
|||
} |
|||
bool is_prime(int candidate) |
|||
{ |
|||
if( (candidate-1)->next_prime() == candidate ) |
|||
return true; |
|||
return false; |
|||
} |
|||
bool is_lt_prime(int p) |
|||
{ |
{ |
||
while(p) { |
while(p) { |
||
if( ! |
if( !p->probably_prime_p() ) |
||
return false; |
return false; |
||
if(direction == "l") |
|||
p = (int)p->digits()[1..]; |
|||
} |
|||
else |
|||
return true; |
|||
⚫ | |||
} |
|||
bool is_rt_prime(int p) |
|||
{ |
|||
while(p) { |
|||
if( !is_prime(p) ) |
|||
return false; |
|||
⚫ | |||
} |
} |
||
return true; |
return true; |
||
Line 2,361: | Line 2,335: | ||
void main() |
void main() |
||
{ |
{ |
||
array primes = get_primes( 10->pow(6) ); |
|||
bool ltp_found, rtp_found; |
bool ltp_found, rtp_found; |
||
for(int prime = 10->pow(6); prime--; prime > 0) { |
|||
if( !ltp_found && |
if( !ltp_found && is_trunc_prime(prime, "l") ) { |
||
ltp_found = true; |
ltp_found = true; |
||
write("Largest LTP: %d\n", prime); |
write("Largest LTP: %d\n", prime); |
||
} |
} |
||
if( !rtp_found && |
if( !rtp_found && is_trunc_prime(prime, "r") ) { |
||
rtp_found = true; |
rtp_found = true; |
||
write("Largest RTP: %d\n", prime); |
write("Largest RTP: %d\n", prime); |
||
Line 2,375: | Line 2,348: | ||
break; |
break; |
||
} |
} |
||
⚫ | |||
} |
|||
Output: |
|||
⚫ | |||
<pre> |
|||
Largest LTP: 999907 |
|||
Largest RTP: 739399 |
|||
⚫ | |||
=={{header|PL/I}}== |
=={{header|PL/I}}== |