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.
For example :
p3 = 2 × 3 × 5 = 30 p4 = 2 × 3 × 5 × 7 = 210 p5 = 2 × 3 × 5 × 7 × 11 = 2310
Now, the smallest difference m between the prime factorial pn and the first prime number greater than pn for which (m > 1), is a prime number.
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 Input : n = 5 Output : 23
Approach : To find the nth Fortunate number, calculate the product of the first n prime numbers (primorial). Let this product be p. Then we find prime number greater than p and return the difference between the found prime number and p.
p4 + 13 = 223, where m = 13, a fortunate number p5 + 23 = 2333, where m = 23, a fortunate number p6 + 17 = 30047, where m = 17, a fortunate number
Optimization : The above solution can be optimized using Sieve of Eratosthenes.
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Find the largest good number in the divisors of given number N
- Find the total number of composite factor for a given number
- Find the number of jumps to reach X in the number line from zero
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the smallest number whose digits multiply to a given number n
- Find the maximum number of composite summands of a number
- Find maximum number that can be formed using digits of a given number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find count of digits in a number that divide the number
- Find if a number is divisible by every number in a list
- Find M-th number whose repeated sum of digits of a number is N
- Find n'th number in a number system with only 3 and 4
- Find the other number when LCM and HCF given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.