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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

Primorial numbers are those formed by multiplying successive prime numbers.

The primorial number series is: To express this mathematically,   primorialn   is   the product of the first   n   (successive) primes:

In some sense, generating primorial numbers is similar to factorials. As with factorials, primorial numbers get large quickly.

By   length   (above), it is meant the number of decimal digits in the numbers.

Let's start with the solution:

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

This Python program uses the pyprimes library to generate the first 1,000,001 prime numbers and calculate the first 10 primorials, which are the products of the first n prime numbers. It also calculates and prints the number of digits in primorials up to 10^7. Here's a breakdown of the code:

  1. Importing Libraries:

    from pyprimes import nprimes
    from functools import reduce

    This code imports the nprimes function from the pyprimes library to generate prime numbers and the reduce function from the functools library to compute the product of a list of numbers.

  2. Generating Prime Numbers:

    primelist = list(nprimes(1000001))  # [2, 3, 5, ...]

    It initializes primelist with the first 1,000,001 prime numbers generated by the nprimes function.

  3. Primorial Function:

    def primorial(n):
       return reduce(int.__mul__, primelist[:n], 1)

    This function calculates the primorial of a given integer n. It multiplies the first n prime numbers in primelist using the reduce function with an initial value of 1.

  4. Printing the First Ten Primorials:

    if __name__ == '__main__':
       print('First ten primorals:', [primorial(n) for n in range(10)])

    This part of the code executes only when the script is run directly (not imported as a module). It prints the first 10 primorials by applying the primorial function to numbers from 1 to 10 and displays them in a list.

  5. Calculating Digit Counts:

    for e in range(7):
       n = 10**e
       print('primorial(%i) has %i digits' % (n, len(str(primorial(n)))))

    This loop iterates through powers of 10 from 10^0 to 10^6 and calculates the number of digits in the primorial of each power. It then prints the result, showing the number of digits in each primorial.

Source code in the python programming language

from pyprimes import nprimes
from functools import reduce


primelist = list(nprimes(1000001))    # [2, 3, 5, ...]

def primorial(n):
    return reduce(int.__mul__, primelist[:n], 1)

if __name__ == '__main__':
    print('First ten primorals:', [primorial(n) for n in range(10)])
    for e in range(7):
        n = 10**e
        print('primorial(%i) has %i digits' % (n, len(str(primorial(n)))))


  

You may also check:How to resolve the algorithm Power set step by step in the Elixir programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Scheme programming language
You may also check:How to resolve the algorithm Null object step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sockets step by step in the OCaml programming language