How to resolve the algorithm Truncatable primes step by step in the Maple programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Truncatable primes step by step in the Maple programming language
Table of Contents
Problem Statement
A truncatable prime is a prime number that when you successively remove digits from one end of the prime, you are left with a new prime number.
The number 997 is called a left-truncatable prime as the numbers 997, 97, and 7 are all prime. The number 7393 is a right-truncatable prime as the numbers 7393, 739, 73, and 7 formed by removing digits from its right are also prime. No zeroes are allowed in truncatable primes.
The task is to find the largest left-truncatable and right-truncatable primes less than one million (base 10 is implied).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Truncatable primes step by step in the Maple programming language
Source code in the maple programming language
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;
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Burlesque programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Lasso programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the Wren programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Delphi programming language