Truncatable primes: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
(Added Pike implementation) |
||
Line 2,318: | Line 2,318: | ||
Output: |
Output: |
||
<pre>-> (998443 . 739399)</pre> |
<pre>-> (998443 . 739399)</pre> |
||
=={{header|Pike}}== |
|||
<lang Pike> |
|||
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) { |
|||
if( !is_prime(p) ) |
|||
return false; |
|||
p = (int)p->digits()[1..]; |
|||
} |
|||
return true; |
|||
} |
|||
bool is_rt_prime(int p) |
|||
{ |
|||
while(p) { |
|||
if( !is_prime(p) ) |
|||
return false; |
|||
p = (int)p->digits()[..<1]; |
|||
} |
|||
return true; |
|||
} |
|||
void main() |
|||
{ |
|||
array primes = get_primes( 10->pow(6) ); |
|||
bool ltp_found, rtp_found; |
|||
foreach( reverse(primes), int prime ) { |
|||
if( !ltp_found && is_lt_prime(prime) ) { |
|||
ltp_found = true; |
|||
write("Largest LTP: %d\n", prime); |
|||
} |
|||
if( !rtp_found && is_rt_prime(prime) ) { |
|||
rtp_found = true; |
|||
write("Largest RTP: %d\n", prime); |
|||
} |
|||
if(ltp_found && rtp_found) |
|||
break; |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |