Truncatable primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Ring}}: Remove vanity tags) |
|||
Line 828: | Line 828: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 3. |
ELENA 3.4 : |
||
<lang elena>import system'calendar. |
<lang elena>import system'calendar. |
||
import extensions. |
import extensions. |
||
const MAXN = 1000000. |
const int MAXN = 1000000. |
||
extension mathOp |
extension<int> mathOp |
||
{ |
{ |
||
isPrime |
bool isPrime |
||
[ |
[ |
||
if (self < 2) [ ^ false. ]. |
|||
⚫ | |||
if ( |
if (self mod:2 == 0) [ ^ false. ]. |
||
if ( |
if (self < 9) [ ^ true. ]. |
||
if ( |
if (self mod:3 == 0) [ ^ false. ]. |
||
⚫ | |||
int r := self sqrt. |
|||
int r := n sqrt. |
|||
int f := 5. |
int f := 5. |
||
while (f <= r) |
while (f <= r) |
||
[ |
[ |
||
if (( |
if ((self mod:f == 0) || (self mod:(f + 2) == 0)) |
||
[ ^ false ]. |
[ ^ false ]. |
||
f := f + 6 |
f := f + 6 |
||
]. |
]. |
||
^ true |
^ true |
||
] |
] |
||
isRightTruncatable |
bool isRightTruncatable |
||
[ |
[ |
||
while (self != 0) |
|||
while (n != 0) |
|||
[ |
[ |
||
ifnot ( |
ifnot (self isPrime) |
||
[ ^ false ]. |
[ ^ false ]. |
||
self := self / 10 |
|||
]. |
]. |
||
^ true. |
^ true. |
||
] |
] |
||
isLeftTruncatable |
bool isLeftTruncatable |
||
[ |
[ |
||
int n := self. |
|||
int tens := 1. |
int tens := 1. |
||
while (tens < |
while (tens < self) |
||
[ tens := tens * 10. ]. |
[ tens := tens * 10. ]. |
||
while ( |
while (self != 0) |
||
[ |
[ |
||
ifnot ( |
ifnot (self isPrime) |
||
[ ^ false ]. |
[ ^ false ]. |
||
tens := tens / 10. |
tens := tens / 10. |
||
self := self - (self / tens * tens) |
|||
]. |
]. |
||
^ true |
^ true |
||
] |
] |
||
} |
} |
||
public program |
|||
[ |
[ |
||
int n := MAXN. |
|||
int max_lt := 0. |
|||
int max_rt := 0. |
|||
while ((max_lt == 0) || (max_rt == 0)) |
while ((max_lt == 0) || (max_rt == 0)) |
||
[ |
[ |
||
if(n literal; indexOf:"0" == -1) |
if(n literal; indexOf:"0" == -1) |
||
[ |
[ |
||
if ((max_lt == 0) && |
if ((max_lt == 0) && (n isLeftTruncatable)) |
||
[ |
[ |
||
max_lt := n. |
max_lt := n. |
||
]. |
]. |
||
if ((max_rt == 0) && |
if ((max_rt == 0) && (n isRightTruncatable)) |
||
[ |
[ |
||
max_rt := n. |
max_rt := n. |
||
]. |
]. |
||
]. |
]. |
||
n := n - 1. |
n := n - 1. |
||
]. |
]. |
||
console printLine("Largest truncable left is ",max_lt). |
console printLine("Largest truncable left is ",max_lt). |
||
console printLine("Largest truncable right is ",max_rt). |
console printLine("Largest truncable right is ",max_rt). |
||
console readChar |
console readChar |
||
] |
]</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |