How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Mathematica / Wolfram Language programming language
How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Mathematica / Wolfram Language 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 Mathematica / Wolfram Language programming language
Purpose:
This Wolfram code finds the largest left-truncatable prime in a given base n
.
Explanation:
Function Definition LargestLeftTruncatablePrimeInBase
:
- Takes an integer
n
representing the base. - Returns the largest prime number in base
n
that can be formed by successively truncating digits from the left.
Implementation:
-
NestWhile[{Select[...], n #[[2]]}
:Select[...]:
Selects all numbers in the range[1, n-1]
that are prime when multiplied by the current primen #[[2]]
.n #[[2]]
: Multiplies the current prime byn
and returns the next number to test.
-
{{0}, 1}
:- Initializes the nested iteration with an empty list
{}
for the truncated digits and 1 as the first prime to test.
- Initializes the nested iteration with an empty list
-
#[[1]] != {}
:- Condition for the
NestWhile
loop. Stops when no more digits can be truncated (i.e.,#[[1]]
is not empty).
- Condition for the
-
Max[...]
:- Returns the maximum of the list of prime numbers generated by the
NestWhile
loop.
- Returns the maximum of the list of prime numbers generated by the
Usage:
The code then uses the Do
loop to print the largest left-truncatable primes for bases 3 to 17.
Example Output:
3 11
4 11
5 22
6 23
7 23
8 27
9 31
10 37
11 41
12 43
13 47
14 53
15 59
16 61
17 67
Source code in the wolfram programming language
LargestLeftTruncatablePrimeInBase[n_] :=
Max[NestWhile[{Select[
Flatten@Outer[Function[{a, b}, #[[2]] a + b],
Range[1, n - 1], #[[1]]], PrimeQ], n #[[2]]} &, {{0},
1}, #[[1]] != {} &, 1, Infinity, -1][[1]]]
Do[Print[n, "\t", LargestLeftTruncatablePrimeInBase@n], {n, 3, 17}]
You may also check:How to resolve the algorithm 100 doors step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Go programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the J programming language
You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the RPL programming language