How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Nim programming language
How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Nim 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 Nim programming language
Source code in the nim programming language
import bignum, strformat
const
Primes = [2, 3, 5, 7, 11, 13, 17]
Digits = "0123456789abcdefghijklmnopqrstuvwxyz"
#---------------------------------------------------------------------------------------------------
func isProbablyPrime(n: Int): bool =
## Return true if "n" is not definitively composite.
probablyPrime(n, 25) != 0
#---------------------------------------------------------------------------------------------------
func maxLeftTruncablePrime(base: int): Int =
## Return the maximum left truncable prime for given base.
let base = base.int32
var primes: seq[Int]
# Initialize primes with one digit in given base.
for p in Primes:
if p < base:
primes.add(newInt(p))
else:
break
# Build prime list with one more digit per generation.
var next: seq[Int]
while true:
# Build the next generation (with one more digit).
for p in primes:
var pstr = ' ' & `$`(p, base) # ' ' as a placeholder for next digit.
for i in 1..<base:
pstr[0] = Digits[i]
let n = newInt(pstr, base)
if n.isProbablyPrime():
next.add(n)
if next.len == 0:
# No primes with this number of digits.
# Return the greatest prime in previous generation.
return max(primes)
# Prepare to build next generation.
primes = next
next.setLen(0)
#———————————————————————————————————————————————————————————————————————————————————————————————————
echo "Base Greatest left truncable prime"
echo "====================================="
for base in 3..17:
let m = maxLeftTruncablePrime(base)
echo &"{base:>3} {m}", if base > 10: " (" & `$`(m, base.int32) & ')' else: ""
You may also check:How to resolve the algorithm Four bit adder step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the zkl programming language
You may also check:How to resolve the algorithm Here document step by step in the BaCon programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the Smalltalk programming language