How to resolve the algorithm Fortunate numbers step by step in the Python programming language
How to resolve the algorithm Fortunate numbers step by step in the Python programming language
Table of Contents
Problem Statement
A Fortunate number is the smallest integer m > 1 such that for a given positive integer n, primorial(n) + m is a prime number, where primorial(n) is the product of the first n prime numbers. For example the first fortunate number is 3 because primorial(1) is 2 and 2 + 3 = 5 which is prime whereas 2 + 2 = 4 is composite.
After sorting and removal of any duplicates, compute and show on this page the first 8 Fortunate numbers or, if your language supports big integers, the first 50 Fortunate numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fortunate numbers step by step in the Python programming language
The given Python program finds and prints the first 50 fortunate numbers. A fortunate number is a positive integer that, when added to the primorial of its position in the sequence of positive integers, produces a prime number.
Key Concepts:
- Primorial: The primorial of a natural number n is the product of the first n prime numbers.
Function fortunate_number:
- This function takes a positive integer n as input and returns the fortunate number for that position.
- It begins by observing that the primorial of any positive integer is even. This eliminates even integers as potential fortunate numbers.
- It then starts searching for fortunate numbers from the odd integer 3.
- It calculates the primorial of n, denoted as primorial_, and iterates over odd integers, starting from 3.
- For each odd integer i, it checks if (primorial_ + i) is a prime number using sympy's isprime function. If it is prime, it returns i as the fortunate number for position n.
Main Program:
- The program initializes an empty set called fortunate_numbers to store the fortunate numbers.
- It iterates from 1 to 75, calling the fortunate_number function for each value of i and adding the result to the set.
- The program extracts the first 50 numbers from the set by sorting them and slicing the list.
- Finally, it prints the first 50 fortunate numbers in a tabular format, with 10 numbers per row.
Source code in the python programming language
from sympy.ntheory.generate import primorial
from sympy.ntheory import isprime
def fortunate_number(n):
'''Return the fortunate number for positive integer n.'''
# Since primorial(n) is even for all positive integers n,
# it suffices to search for the fortunate numbers among odd integers.
i = 3
primorial_ = primorial(n)
while True:
if isprime(primorial_ + i):
return i
i += 2
fortunate_numbers = set()
for i in range(1, 76):
fortunate_numbers.add(fortunate_number(i))
# Extract the first 50 numbers.
first50 = sorted(list(fortunate_numbers))[:50]
print('The first 50 fortunate numbers:')
print(('{:<3} ' * 10).format(*(first50[:10])))
print(('{:<3} ' * 10).format(*(first50[10:20])))
print(('{:<3} ' * 10).format(*(first50[20:30])))
print(('{:<3} ' * 10).format(*(first50[30:40])))
print(('{:<3} ' * 10).format(*(first50[40:])))
You may also check:How to resolve the algorithm Execute a system command step by step in the DCL programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Balanced ternary step by step in the Wren programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the Maxima programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the AWK programming language