aboutsummaryrefslogtreecommitdiff
path: root/challenge-155/paulo-custodio/python/ch-1.py
blob: e1cdaebf7612f6fe4471e80a29935365fb2e2099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3

# Challenge 155
#
# TASK #1 > Fortunate Numbers
# Submitted by: Mohammad S Anwar
# Write a script to produce first 8 Fortunate Numbers (unique and sorted).
#
# According to Wikipedia
#
# A Fortunate number, named after Reo Fortune, is the smallest integer m > 1
# such that, for a given positive integer n, pn# + m is a prime number, where
# the primorial pn# is the product of the first n prime numbers.
#
# Expected Output
# 3, 5, 7, 13, 17, 19, 23, 37

from math import prod
from sympy import isprime, nextprime

fortunate = {}

primes = [2]
while len(fortunate) < 8:
    p = prod(primes)
    m = 2
    while not isprime(p + m):
        m += 1
    fortunate[m] = 1
    primes.append(nextprime(primes[-1]))

print(", ".join(map(str, sorted(fortunate.keys()))))