50 lines
1.5 KiB
Text
50 lines
1.5 KiB
Text
MaxTruncatablePrime := proc({left::truefalse:=FAIL, right::truefalse:=FAIL}, $)
|
|
local i, j, c, p, b, n, sdprimes, dir;
|
|
local tprimes := table();
|
|
if left = true and right = true then
|
|
error "invalid input";
|
|
elif right = true then
|
|
dir := "right";
|
|
else
|
|
dir := "left";
|
|
end if;
|
|
b := 10;
|
|
n := 6;
|
|
sdprimes := select(isprime, [seq(1..b-1)]);
|
|
for p in sdprimes do
|
|
if assigned(tprimes[p]) then
|
|
next;
|
|
end if;
|
|
i := ilog[b](p)+1;
|
|
j := 1;
|
|
while p < b^n do
|
|
if dir = "left" then
|
|
c := j*b^i + p;
|
|
else
|
|
c := p*b + j;
|
|
end if;
|
|
if j >= b or c > b^n then # we have tried all 1 digit extensions of p, add p to tprimes and move back 1 digit
|
|
tprimes[p] := p;
|
|
if i = 1 then # if we are at the first digit, go to the next 1 digit prime
|
|
break;
|
|
end if;
|
|
i := i - 1;
|
|
j := 1;
|
|
if dir = "left" then
|
|
p := p - iquo(p, b^i)*b^i;
|
|
else
|
|
p := iquo(p, b);
|
|
end if;
|
|
elif assigned(tprimes[c]) then
|
|
j := j + 1;
|
|
elif isprime(c) then
|
|
p := c;
|
|
i := i + 1;
|
|
j := 1;
|
|
else
|
|
j := j+1;
|
|
end if;
|
|
end do;
|
|
end do;
|
|
return max(indices(tprimes, 'nolist'));
|
|
end proc;
|