How to resolve the algorithm Truncatable primes step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Truncatable primes step by step in the Python 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 Python programming language

The given Python code finds the longest left and right truncatable primes below a specified limit maxprime. A truncatable prime is a prime number that remains a prime when its digits are removed from either end.

The code uses a helper function primes(n) to generate a list of prime numbers up to n. It uses the Sieve of Eratosthenes algorithm to efficiently find prime numbers.

The main function truncatableprime(n) takes n as input and returns the longest left and right truncatable primes below n. It uses the list of prime numbers generated by primes(n) to find the longest truncatable primes.

The function iterates through the list of prime numbers in reverse order (largest to smallest) and checks if the prime number is a left truncatable prime. To check if a prime number is a left truncatable prime, the function generates a set of all possible truncations of the prime number (by removing digits from the left end) and checks if all the truncations are also prime numbers. If all the truncations are prime numbers, the function stores the prime number in the truncateleft variable.

The function then iterates through the list of prime numbers again in reverse order and checks if the prime number is a right truncatable prime. To check if a prime number is a right truncatable prime, the function generates a set of all possible truncations of the prime number (by removing digits from the right end) and checks if all the truncations are also prime numbers. If all the truncations are prime numbers, the function stores the prime number in the truncateright variable.

Finally, the function returns the truncateleft and truncateright values as a tuple.

The code prints the longest left and right truncatable primes below maxprime as a tuple.

Source code in the python programming language

maxprime = 1000000

def primes(n):
    multiples = set()
    prime = []
    for i in range(2, n+1):
        if i not in multiples:
            prime.append(i)
            multiples.update(set(range(i*i, n+1, i)))
    return prime

def truncatableprime(n):
    'Return a longest left and right truncatable primes below n'
    primelist = [str(x) for x in primes(n)[::-1]]
    primeset = set(primelist)
    for n in primelist:
        # n = 'abc'; [n[i:] for i in range(len(n))] -> ['abc', 'bc', 'c']
        alltruncs = set(n[i:] for i in range(len(n)))
        if alltruncs.issubset(primeset):
            truncateleft = int(n)
            break
    for n in primelist:
        # n = 'abc'; [n[:i+1] for i in range(len(n))] -> ['a', 'ab', 'abc']
        alltruncs = set([n[:i+1] for i in range(len(n))])
        if alltruncs.issubset(primeset):
            truncateright = int(n)
            break
    return truncateleft, truncateright

print(truncatableprime(maxprime))


  

You may also check:How to resolve the algorithm Knuth shuffle step by step in the Fantom programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Harbour programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Joy programming language
You may also check:How to resolve the algorithm Date manipulation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Nth root step by step in the Seed7 programming language