Find n-th Fortunate Number


Fortunate Numbers − It is the smallest integer m > 1 such that, for a given positive integer n, pn# + m is a prime number, where pn# is the product of the first n prime numbers.

For example, for calculating the third fortunate number, first calculate the product of the first 3 prime numbers (2, 3, 5) i.e. 30. Upon adding 2 we get 32 which is an even number, adding 3 gives 33 which is a multiple of 3. One would similarly rule out integers up to 6. Adding 7 gives, 37 which is a prime number. Thus, 7 is the third fortunate number.

Fortunate numbers for the first primorials are −

3, 5, 7, 13, 23, 17, 19, 23, 37, 61, 67, 61, 71, 47, 107, 59, 61, 109 ….

Problem Statement

Given a number n. Find the nth fortunate number.

Sample Example 1

Input: n = 3
Output: 7

Explanation − Product of first 3 price numbers −

2  3  5 = 30
30 + 7 = 37, a prime number.

Sample Example 2

Input: n = 7
Output: 19

Explanation − Product of first 7 prime numbers −

2  3  5  7  11  13  17 = 510510
510510 + 19 = 510529, a prime number.

Approach 1: Primorial Method

A simple solution to the problem is first to calculate pn# i.e. product of the first n prime numbers and then find the difference between the pn# and the next prime number. The difference obtained will be a fortunate number.

Pseudocode

procedure prime (num)
   if num <= 1
      ans = TRUE
   end if
   for i = 2 to sqrt(num)
      if i is a factor of num
         ans = false
      end if
   ans = true
end procedure
procedure nthFortunate (n)
   prod = 1
   count = 0
   for i = 2 to count < n
      if i is prime
         prod = prod * i
         count = count + 1
      end if
   nextPrime = prod + 2
   while nextPrime is not prime
      nextPrime = next Prime + 1
   ans = nextPrime - prod
end procedure

Example: C++ Implementation

In the following program, the fortunate number is calculated by calculating the primordial of the first n prime numbers and the next prime number after primorial is found. The fortunate number is the difference between the next prime and the primorial.

#include <bits/stdc++.h>
using namespace std;

// Function to find if a number is prime or not
bool prime(unsigned long long int num){
   if (num <= 1)
      return true;
   for (int i = 2; i <= sqrt(num); i++){
      if (num % i == 0)
         return false;
   }
   return true;
}

// Function to find the nth Fortunate number
unsigned long long int nthFortunate(int n){
   long long int prod = 1, count = 0;
   
   // Calculating product/primorial of first n prime numbers
   for (int i = 2; count < n; i++){
      if (prime(i)){
         prod *= i;
         count++;
      }
   }
   
   // Find the next prime greater than the product of n prime numbers
   unsigned long long int nextPrime = prod + 2;
   while (!prime(nextPrime)){
      nextPrime++;
   }
   
   // Fortunate number is the difference between prime and primorial
   unsigned long long int ans = nextPrime - prod;
   return ans;
}
int main(){
   int n = 15;
   cout << n << "th Fortunate number : " << nthFortunate(n);
   return 0;
}

Output

15th Fortunate number : 107

Time Complexity − O(nsqrt(n)), where prime() function has complexity O(sqrt(n)), for loop in nthFortunate() has the complexity of O(nsqrt(n)).

Space Complexity − O(1)

Approach 2: Sieve of Eratosthenes

The sieve of Eratosthenes is used to get all the prime numbers up to a limit which we will give a value MAX. In this approach, we create a boolean array with all true entries and mark all non-prime indexes as false. Then multiplying the first n prime numbers from the array to get the product of the first n prime numbers. Then similar to the previous approach increment product by 1 starting with 2 to get the next prime number. The difference between the next prime number and the product will be the fortunate number required.

Pseudocode

procedure nthFortunate (n)
   MAX is set
   prime[MAX] = {true}
   prime[0] = false
   prime[1] = false
   for i = 1 to i*i <= MAX
      if prime[i]
         for j = i*i to MAX with j = j + i in each iteration
            prime [j] = false
      end if
   prod = 1
   count = 0
   for i = 2 to count < n
      if prime[i]
         prod = prod * i
         count = count + 1
      end if
   nextPrime = prod + 2
   while nextPrime is not prime
      nextPrime = nextPrime + 1
   ans = nextPrime - prod
end procedure

Example: C++ Implementation

In the following program, the boolean prime array of the size MAX keeps a record of all the prime numbers up to MAX. Then the primorial is found by multiplying the first n prime numbers. Then similar to the previous approach, nextPrime is found. The difference between nextPrime and product is the fortunate number.

#include <bits/stdc++.h>
using namespace std;

// Function to find the nth Fortunate number
unsigned long long int nthFortunate(int n){

   // Setting upper limit for Sieve of Eratosthenes
   const unsigned long long int MAX = 1000000000;
   vector<bool> prime(MAX, true);
   prime[0] = prime[1] = false;
   
   // Sieve of Eratosthenes to find all primes up to MAX
   for (unsigned long long int i = 2; i * i <= MAX; i++){
      if (prime[i]){
      
         // Setting all the multiples of i to false
         for (int j = i * i; j <= MAX; j += i){
            prime[j] = false;
         }
      }
   }
   
   // Find the first n primes and calculate their product
   unsigned long long int prod = 1, count = 0;
   for (unsigned long long int i = 2; count < n; i++){
      if (prime[i]){
         prod *= i;
         count++;
      }
   }
   
   // Find next prime greater than product
   unsigned long long int nextPrime = prod + 2;
   while (!prime[nextPrime])
      nextPrime++;
      
   // Fortunate number is difference between prime and product
   return nextPrime - prod;
}
int main(){
   int n = 25;
   cout << n << "th Fortunate number : " << nthFortunate(n);
   return 0;
}

Output

15th Fortunate number : 107

Time Complexity − O(n log(log(n)))

Space Complexity − O(MAX)

Conclusion

In conclusion, nth Fortunate number can be found by the following two approaches.

The primorial method: Find the product of the first n prime numbers and calculate the next prime number from the product. The difference between the prime and the product is the nth Fortunate number.

The sieve of Eratosthenes: Finding all prime numbers up to a limit and then calculating the product and next prime number to find the fortunate number.

Both methods are efficient for small values of n only due to constraints on the size of variables. For larger values, a more efficient and optimized solution is required.

Updated on: 25-Jul-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements