How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Maple programming language
How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Maple programming language
Table of Contents
Problem Statement
A truncatable prime is one where all non-empty substrings that finish at the end of the number (right-substrings) are also primes when understood as numbers in a particular base. The largest such prime in a given (integer) base is therefore computable, provided the base is larger than 2. Let's consider what happens in base 10. Obviously the right most digit must be prime, so in base 10 candidates are 2,3,5,7. Putting a digit in the range 1 to base-1 in front of each candidate must result in a prime. So 2 and 5, like the whale and the petunias in The Hitchhiker's Guide to the Galaxy, come into existence only to be extinguished before they have time to realize it, because 2 and 5 preceded by any digit in the range 1 to base-1 is not prime. Some numbers formed by preceding 3 or 7 by a digit in the range 1 to base-1 are prime. So 13,17,23,37,43,47,53,67,73,83,97 are candidates. Again, putting a digit in the range 1 to base-1 in front of each candidate must be a prime. Repeating until there are no larger candidates finds the largest left truncatable prime. Let's work base 3 by hand: 0 and 1 are not prime so the last digit must be 2. 123 = 510 which is prime, 223 = 810 which is not so 123 is the only candidate. 1123 = 1410 which is not prime, 2123 = 2310 which is, so 2123 is the only candidate. 12123 = 5010 which is not prime, 22123 = 7710 which also is not prime. So there are no more candidates, therefore 23 is the largest left truncatable prime in base 3. The task is to reconstruct as much, and possibly more, of the table in the OEIS as you are able. Related Tasks:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Maple programming language
Source code in the maple programming language
MaxLeftTruncatablePrime := proc(b, $)
local i, j, c, p, sdprimes;
local tprimes := table();
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;
do
c := j*b^i + p;
if j >= b 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;
p := p - iquo(p, b^i)*b^i;
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;
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the GEORGE programming language
You may also check:How to resolve the algorithm Go Fish step by step in the Raku programming language
You may also check:How to resolve the algorithm Null object step by step in the Sidef programming language
You may also check:How to resolve the algorithm Window creation step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the AppleScript programming language