Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
94
Task/Truncatable-primes/Elena/truncatable-primes.elena
Normal file
94
Task/Truncatable-primes/Elena/truncatable-primes.elena
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import extensions;
|
||||
|
||||
const MAXN = 1000000;
|
||||
|
||||
extension mathOp
|
||||
{
|
||||
isPrime()
|
||||
{
|
||||
int n := cast int(self);
|
||||
|
||||
if (n < 2) { ^ false };
|
||||
if (n < 4) { ^ true };
|
||||
if (n.mod:2 == 0) { ^ false };
|
||||
if (n < 9) { ^ true };
|
||||
if (n.mod:3 == 0) { ^ false };
|
||||
|
||||
int r := n.sqrt();
|
||||
int f := 5;
|
||||
while (f <= r)
|
||||
{
|
||||
if ((n.mod(f) == 0) || (n.mod(f + 2) == 0))
|
||||
{ ^ false };
|
||||
|
||||
f := f + 6
|
||||
};
|
||||
|
||||
^ true
|
||||
}
|
||||
|
||||
isRightTruncatable()
|
||||
{
|
||||
int n := self;
|
||||
|
||||
while (n != 0)
|
||||
{
|
||||
ifnot (n.isPrime())
|
||||
{ ^ false };
|
||||
|
||||
n := n / 10
|
||||
};
|
||||
|
||||
^ true
|
||||
}
|
||||
|
||||
isLeftTruncatable()
|
||||
{
|
||||
int n := self;
|
||||
int tens := 1;
|
||||
|
||||
while (tens < n)
|
||||
{ tens := tens * 10 };
|
||||
|
||||
while (n != 0)
|
||||
{
|
||||
ifnot (n.isPrime())
|
||||
{ ^ false };
|
||||
|
||||
tens := tens / 10;
|
||||
n := n - (n / tens * tens)
|
||||
};
|
||||
|
||||
^ true
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var n := MAXN;
|
||||
var max_lt := 0;
|
||||
var max_rt := 0;
|
||||
|
||||
while (max_lt == 0 || max_rt == 0)
|
||||
{
|
||||
if(n.toString().indexOf("0") == -1)
|
||||
{
|
||||
if ((max_lt == 0) && (n.isLeftTruncatable()))
|
||||
{
|
||||
max_lt := n
|
||||
};
|
||||
|
||||
if ((max_rt == 0) && (n.isRightTruncatable()))
|
||||
{
|
||||
max_rt := n
|
||||
}
|
||||
};
|
||||
|
||||
n := n - 1
|
||||
};
|
||||
|
||||
console.printLine("Largest truncable left is ",max_lt);
|
||||
console.printLine("Largest truncable right is ",max_rt);
|
||||
|
||||
console.readChar()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue