How to resolve the algorithm Unprimeable numbers step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Unprimeable numbers step by step in the Python programming language

Table of Contents

Problem Statement

As used here, all unprimeable numbers   (positive integers)   are always expressed in base ten.

───── Definition from OEIS ─────: Unprimeable numbers are composite numbers that always remain composite when a single decimal digit of the number is changed.

───── Definition from Wiktionary   (referenced from Adam Spencer's book) ─────: (arithmetic)   that cannot be turned into a prime number by changing just one of its digits to any other digit.   (sic)

Unprimeable numbers are also spelled:   unprimable. All one─ and two─digit numbers can be turned into primes by changing a single decimal digit.

190   isn't unprimeable,   because by changing the zero digit into a three yields   193,   which is a prime.

The number   200   is unprimeable,   since none of the numbers   201, 202, 203, ··· 209   are prime, and all the other numbers obtained by changing a single digit to produce   100, 300, 400, ··· 900,   or   210, 220, 230, ··· 290   which are all even.

It is valid to change   189   into   089   by changing the   1   (one)   into a   0   (zero),   which then the leading zero can be removed,   and then treated as if the   "new"   number is   89.

Show all output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Unprimeable numbers step by step in the Python programming language

This Python code generates unprime numbers. Unprime numbers are positive integers where the removal of any single digit results in a prime number. The code uses a generator function called unprime to yield unprime numbers.

Here's a detailed explanation of the code:

  1. Prime Number Generation: The code uses a generator function called primes to generate prime numbers on demand. It uses a list called _cache to store prime numbers generated so far. The primes function yields from the _cache initially and then generates new prime numbers using the isprime function.

  2. Primality Check: The isprime function checks if a given integer n is prime. It uses a memoization technique to store primality results in a dictionary called _seen. The function iterates over the prime numbers generated so far and checks if n is divisible by any of them. If it is, n is not prime, and the function returns False. Otherwise, it returns True.

  3. Unprime Number Generation: The unprime function generates unprime numbers. It iterates over positive integers indefinitely. For each integer a, it checks if the removal of any single digit from a results in a prime number. If it does, a is an unprime number, and it is yielded.

  4. Printing the First 35 Unprime Numbers: The code uses the islice function to print the first 35 unprime numbers. It prints them in a space-separated list.

  5. Finding the 600th Unprime Number: The code also prints the 600th unprime number. It uses the islice function to skip the first 599 unprime numbers and then prints the 600th one.

  6. Finding Unprime Numbers Ending in Different Digits: The code finds unprime numbers ending in each digit from 0 to 9. It stores the first unprime number ending in each digit in a list called first. It then prints the digit and the corresponding unprime number.

Source code in the python programming language

from itertools import count, islice

def primes(_cache=[2, 3]):
    yield from _cache
    for n in count(_cache[-1]+2, 2):
        if isprime(n):
            _cache.append(n)
            yield n

def isprime(n, _seen={0: False, 1: False}):
    def _isprime(n):
        for p in primes():
            if p*p > n:
                return True
            if n%p == 0:
                return False

    if n not in _seen:
        _seen[n] = _isprime(n)
    return _seen[n]

def unprime():
    for a in count(1):
        d = 1
        while d <= a:
            base = (a//(d*10))*(d*10) + (a%d) # remove current digit
            if any(isprime(y) for y in range(base, base + d*10, d)):
                break
            d *= 10
        else:
            yield a


print('First 35:')
print(' '.join(str(i) for i in islice(unprime(), 35)))

print('\nThe 600-th:')
print(list(islice(unprime(), 599, 600))[0])
print()

first, need = [False]*10, 10
for p in unprime():
    i = p%10
    if first[i]: continue

    first[i] = p
    need -= 1
    if not need:
        break

for i,v in enumerate(first):
    print(f'{i} ending: {v}')


  

You may also check:How to resolve the algorithm Stirling numbers of the first kind step by step in the Phix programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Raku programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Erlang programming language
You may also check:How to resolve the algorithm Power set step by step in the PL/I programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the Julia programming language