Truncatable primes: Difference between revisions

Content added Content deleted
m (→‎{{header|Ring}}: Remove vanity tags)
Line 828: Line 828:


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.2 :
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
[
[
int n := self int.
if (self < 2) [ ^ false. ].
if (self < 4) [ ^ true. ].
if (n < 2) [ ^ false. ].
if (self mod:2 == 0) [ ^ false. ].
if (n < 4) [ ^ true. ].
if (self < 9) [ ^ true. ].
if (n mod:2 == 0) [ ^ false. ].
if (self mod:3 == 0) [ ^ false. ].
if (n < 9) [ ^ true. ].
if (n 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 ((n mod:f == 0) || (n mod:(f + 2) == 0))
if ((self mod:f == 0) || (self mod:(f + 2) == 0))
[ ^ false ].
[ ^ false ].
f := f + 6
f := f + 6
].
].
^ true
^ true
]
]
isRightTruncatable
bool isRightTruncatable
[
[
int n := self.
while (self != 0)
while (n != 0)
[
[
ifnot (n isPrime)
ifnot (self isPrime)
[ ^ false ].
[ ^ false ].
n := n / 10
self := self / 10
].
].
^ true.
^ true.
]
]

isLeftTruncatable
bool isLeftTruncatable
[
[
int n := self.
int tens := 1.
int tens := 1.
while (tens < n)
while (tens < self)
[ tens := tens * 10. ].
[ tens := tens * 10. ].
while (n != 0)
while (self != 0)
[
[
ifnot (n isPrime)
ifnot (self isPrime)
[ ^ false ].
[ ^ false ].

tens := tens / 10.
tens := tens / 10.
n := n - (n / tens * tens)
self := self - (self / tens * tens)
].
].
^ true
^ true
]
]
}
}

program =
public program
[
[
var n := MAXN.
int n := MAXN.
var max_lt := 0.
int max_lt := 0.
var max_rt := 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) && $(n isLeftTruncatable))
if ((max_lt == 0) && (n isLeftTruncatable))
[
[
max_lt := n.
max_lt := n.
].
].
if ((max_rt == 0) && $(n isRightTruncatable))
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>
]</lang>
{{out}}
{{out}}
<pre>
<pre>