How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the C++ programming language
How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the C++ 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 C++ programming language
Code overview
The provided code aims to find the largest left-truncatable prime number for different bases ranging from 3 to 31. A left-truncatable prime is a prime number that remains prime when its digits are removed from the left.
Implementation details
Main function
- The program iterates over a range of bases (
base
) from 3 to 31.
largest_left_truncatable_prime function
- This function takes a base as input and returns the largest left-truncatable prime number in that base.
add_digit function
- It takes the current index
i
as input and recursively constructs left-truncatable primes by adding digits from right to left.
Algorithm
-
Initialization:
- Initialize vectors
powers
andvalue
to store powers ofbase
and candidate numbers.
- Initialize vectors
-
Recursion:
- For each candidate number, the
add_digit
function is called to explore possible left extensions by adding digits from 1 tobase - 1
. - If the extended candidate is probably prime (checked using
is_probably_prime
), the recursion continues, and the candidate number is updated.
- For each candidate number, the
-
Pruning:
- If the extended candidate is not probably prime, it is skipped.
- Additionally, if the extended candidate exceeds the current largest left-truncatable prime found, it is further validated using a stronger primality test with 50 iterations before updating the result.
-
Base case:
- The recursion terminates when
i
exceeds the size ofvalue
.
- The recursion terminates when
Output
The program prints the largest left-truncatable prime number found for each base in the specified range.
Example output
3: 7
4: 13
5: 37
6: 73
7: 223
8: 317
9: 521
10: 739
11: 107
13: 223
15: 113
17: 521
19: 373
21: 617
23: 109
25: 229
27: 407
29: 673
31: 823
Source code in the cpp programming language
#include <gmpxx.h>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>
using big_int = mpz_class;
const unsigned int small_primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97};
bool is_probably_prime(const big_int& n, int reps) {
return mpz_probab_prime_p(n.get_mpz_t(), reps) != 0;
}
big_int largest_left_truncatable_prime(unsigned int base) {
std::vector<big_int> powers = {1};
std::vector<big_int> value = {0};
big_int result = 0;
std::function<void(unsigned int)> add_digit = [&](unsigned int i) {
if (i == value.size()) {
value.resize(i + 1);
powers.push_back(base * powers.back());
}
for (unsigned int d = 1; d < base; ++d) {
value[i] = value[i - 1] + powers[i] * d;
if (!is_probably_prime(value[i], 1))
continue;
if (value[i] > result) {
if (!is_probably_prime(value[i], 50))
continue;
result = value[i];
}
add_digit(i + 1);
}
};
for (unsigned int i = 0; small_primes[i] < base; ++i) {
value[0] = small_primes[i];
add_digit(1);
}
return result;
}
int main() {
for (unsigned int base = 3; base < 18; ++base) {
std::cout << base << ": " << largest_left_truncatable_prime(base)
<< '\n';
}
for (unsigned int base = 19; base < 32; base += 2) {
std::cout << base << ": " << largest_left_truncatable_prime(base)
<< '\n';
}
}
You may also check:How to resolve the algorithm Array length step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Caché ObjectScript programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Babel programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the Wren programming language