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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A   long prime   (as defined here)   is a prime number whose reciprocal   (in decimal)   has a   period length   of one less than the prime number.

Long primes   are also known as:

Another definition:   primes   p   such that the decimal expansion of   1/p   has period   p-1,   which is the greatest period possible for any integer.

7   is the first long prime,   the reciprocal of seven is   1/7,   which is equal to the repeating decimal fraction   0.142857142857··· The length of the   repeating   part of the decimal fraction is six,   (the underlined part)   which is one less than the (decimal) prime number   7. Thus   7   is a long prime.

There are other (more) general definitions of a   long prime   which include wording/verbiage for bases other than ten.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long primes step by step in the Python programming language

The provided Python code is designed to find and count "long primes" up to a certain limit (64,000 in this case). A "long prime" is defined as a prime number whose reciprocal has a period equal to the prime number minus 1.

Here's a step-by-step breakdown of the code:

  1. Sieve of Eratosthenes (Function sieve): This function implements the classic Sieve of Eratosthenes algorithm to find all prime numbers up to a specified limit. It initializes a list c of booleans, where each element represents a number from 0 to the limit. The algorithm iterates through odd numbers starting from 3, marking multiples of each prime as non-prime ("True" in the c list). This process continues until all numbers within the limit have been processed. The resulting list of prime numbers is stored in the primes list.

  2. Finding the Period of a Reciprocal (Function findPeriod): This function takes a positive integer n as input and calculates the period of its reciprocal. The period is the number of digits that repeat in the decimal expansion of 1/n. The function iteratively multiplies the previous remainder by 10 and takes the remainder when divided by n. It continues this process until the remainder matches the initial remainder, indicating the completion of one period. The count of iterations represents the period length.

  3. Finding Long Primes: The code uses the findPeriod function to identify prime numbers whose reciprocal has a period equal to the prime number minus 1. These primes are considered "long primes" and are stored in the longPrimes list.

  4. Counting Long Primes within Limits: The code defines a list numbers containing limits (500, 1000, 2000, 4000, 8000, 16000, 32000, 64000). It initializes a list totals with the same length to store the count of "long primes" within each limit. It iterates through the longPrimes list and increments a count. Whenever a new limit is encountered, it records the current count in the corresponding totals list and increments the index.

  5. Printing the Results: Finally, the code prints the "long primes" up to 500 and the count of "long primes" within each of the specified limits.

In essence, the code finds "long primes" up to a certain limit, counts them within specified intervals, and reports the results.

Source code in the python programming language

def sieve(limit):
    primes = []
    c = [False] * (limit + 1) # composite = true
    # no need to process even numbers
    p = 3
    while True:
        p2 = p * p
        if p2 > limit: break
        for i in range(p2, limit, 2 * p): c[i] = True
        while True:
            p += 2
            if not c[p]: break

    for i in range(3, limit, 2):
        if not c[i]: primes.append(i)
    return primes

# finds the period of the reciprocal of n
def findPeriod(n):
    r = 1
    for i in range(1, n): r = (10 * r) % n
    rr = r
    period = 0
    while True:
        r = (10 * r) % n
        period += 1
        if r == rr: break
    return period

primes = sieve(64000)
longPrimes = []
for prime in primes:
    if findPeriod(prime) == prime - 1:
        longPrimes.append(prime)
numbers = [500, 1000, 2000, 4000, 8000, 16000, 32000, 64000]
count = 0
index = 0
totals = [0] * len(numbers)
for longPrime in longPrimes:
    if longPrime > numbers[index]:
        totals[index] = count
        index += 1
    count += 1
totals[-1] = count
print('The long primes up to 500 are:')
print(str(longPrimes[:totals[0]]).replace(',', ''))
print('\nThe number of long primes up to:')
for (i, total) in enumerate(totals):
    print('  %5d is %d' % (numbers[i], total))


  

You may also check:How to resolve the algorithm Integer overflow step by step in the Rust programming language
You may also check:How to resolve the algorithm Align columns step by step in the Tcl programming language
You may also check:How to resolve the algorithm One-time pad step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Number names step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Julia programming language