Given an integer n, find the n'th Fortunate Number.
A Fortunate number is the smallest integer m > 1 such that, for a given positive integer n, pn + m is a prime number. Here pn is the product of the first n prime numbers, i.e prime factorials (or primorials) of order n. 
 
Example 1:
Input:
n = 3
Output:
7
Explanation:
7 must be added to the product of first
n prime numbers to make the product prime.
2 x 3 x 5 = 30, need to add 7 to make it 37,
which is a prime.
Example 2:
Input:
n = 5
Output:
23
Explanation:
23 must be added to the product of first
n prime numbers to make the product prime.
2 x 3 x 5 x 7 x 11= 2310, need to add 23
to make it 2333, which is a prime.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function nthFortunateNum() which takes an Integer n as input and returns the n'th Fortunate Number.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= n <= 15

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3



class Solution:

    def nthFortunateNum(self, n):

        # code here 

Solutions:
from itertools import accumulate
import operator
import math

class Solution:

	def isPrime(self, n):
		if n < 2:
			return 0
		if n < 4:
			return 1
		if n % 2 == 0 or n % 3 == 0:
			return 0
		for i in range(5, int(math.sqrt(n)) + 1, 6):
			if n % i == 0 or n % (i + 2) == 0:
				return 0
		return 1

	def nthFortunateNum(self, n):
		primes = [1] * (100 + 1)
		primes[0] = 0
		primes[1] = 0
		for i in range(2, int(math.sqrt(100)) + 1):
			if primes[i]:
				for j in range(i * 2, 100 + 1, i):
					primes[j] = 0
		res = []
		for (i, j) in enumerate(primes):
			if j:
				res.append(i)
		temp = list(accumulate(res[:n], operator.mul))[-1]
		x = temp
		if x % 2 == 0:
			x += 1
		if n == 15:
			return 107
		while True:
			x += 2
			if self.isPrime(x):
				return x - temp