📜  查找第n个幸运数字

📅  最后修改于: 2021-05-07 08:57:22             🧑  作者: Mango

幸运数是m> 1的最小整数,这样,对于给定的正整数n,p n + m是质数。在这里,p n是前n个素数的乘积,即n阶的素数阶乘。
例如 :

p3 = 2 × 3 × 5 = 30
p4 = 2 × 3 × 5 × 7 = 210
p5 = 2 × 3 × 5 × 7 × 11 = 2310

现在,素数阶乘p n与大于p n的第一个素数之间的最小差m(m> 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

Input : n = 5
Output : 23

方法:要找到第n个幸运数,请计算前n个素数(乘数)的乘积。将此产品设为p。然后我们发现素数大于p,并返回找到的素数和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
C++
// C++ program to find n-th Fortunate number
#include 
using namespace std;
 
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
  
    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
  
    return true;
}
 
// Function to Find primorial of order n
// (product of first n prime numbers).
long long int primorial(long long int n)
{
    long long int p = 2;
    n--;
    for (int i = 3; n != 0; i++) {
        if (isPrime(i)) {
            p = p * i;
            n--;
        }
        i++;
    }
    return p;
}
 
// Function to find next prime number greater
// than n
long long int findNextPrime(long long int n)
{
    // Note that difference (or m) should be
    // greater than 1.
    long long int nextPrime = n + 2;
 
    // loop continuously until isPrime
    // returns true for a number above n
    while (true) {
 
        // Ignoring the prime number that
        // is 1 greater than n
        if (isPrime(nextPrime))
            break;
 
        nextPrime++;
    }
 
    return nextPrime;
}
 
// Returns n-th Fortunate number
long long int fortunateNumber(int n)
{
   long long int p = primorial(n);
   return findNextPrime(p) - p;
}
 
// Driver function
int main()
{
    long long int n = 5;
    cout << fortunateNumber(n) << "\n";
    return 0;
}


Java
// Java program to find n-th Fortunate number
import java.lang.*;
import java.util.*;
 
class GFG
{
     
    public static boolean isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
     
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0) return false;
     
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
            return false;
     
        return true;
    }
     
    // Function to Find primorial of order n
    // (product of first n prime numbers).
    public static int primorial(int n)
    {
        int p = 2;
        n--;
        for (int i = 3; n != 0; i++) {
            if (isPrime(i) == true) {
                p = p * i;
                n--;
            }
            i++;
        }
        return p;
    }
 
    // Function to find next prime number greater
    // than n
    public static int findNextPrime(int n)
    {
        // Note that difference (or m) should be
        // greater than 1.
        int nextPrime = n + 2;
     
        // loop continuously until isPrime
        // returns true for a number above n
        while (true) {
     
            // Ignoring the prime number that
            // is 1 greater than n
            if (isPrime(nextPrime) == true)
                break;
     
            nextPrime++;
       }
 
    return nextPrime;
    }
     
    // Returns n-th Fortunate number
    public static int fortunateNumber(int n)
    {
        int p = primorial(n);
        return findNextPrime(p)-p;
    }
     
    //Driver function
    public static void main (String[] args) {
        int n = 5;
        System.out.println(fortunateNumber(n));
    }
}
 
/*This code is contributed by Akash Singh*/


Python3
# Python3 program to find
# n-th Fortunate number
 
def isPrime(n):
 
    # Corner cases
    if (n <= 1): return False
    if (n <= 3): return True
 
    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0):
        return False
     
    i = 5
    while(i * i <= n):
        if (n % i == 0 or
            n % (i + 2) == 0):
            return False
        i += 6
         
    return True
 
 
# Function to Find primorial of order n
# (product of first n prime numbers).
def primorial(n):
 
    p = 2; n -= 1; i = 3
    while(n != 0):
        if (isPrime(i)):
            p = p * i
            n -= 1
         
        i += 1
     
    return p
 
 
# Function to find next prime
# number greater than n
def findNextPrime(n):
 
    # Note that difference (or m)
    # should be greater than 1.
    nextPrime = n + 2
 
    # loop continuously until isPrime
    # returns true for a number above n
    while (True):
 
        # Ignoring the prime number that
        # is 1 greater than n
        if (isPrime(nextPrime)):
            break
 
        nextPrime += 1
     
    return nextPrime
 
# Returns n-th Fortunate number
def fortunateNumber(n):
    p = primorial(n)
    return findNextPrime(p) - p
 
# Driver Code
n = 5
print(fortunateNumber(n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to find
// n-th Fortunate number
using System;
 
class GFG
{
    public static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
     
        // This is checked so that
        // we can skip middle five
        // numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
     
        for (int i = 5;
                 i * i <= n; i = i + 6)
            if (n % i == 0 ||
                n % (i + 2) == 0)
            return false;
     
        return true;
    }
     
    // Function to Find primorial
    // of order n (product of first
    // n prime numbers).
    public static int primorial(int n)
    {
        int p = 2;
        n--;
        for (int i = 3; n != 0; i++)
        {
            if (isPrime(i) == true)
            {
                p = p * i;
                n--;
            }
            i++;
        }
        return p;
    }
 
    // Function to find next
    // prime number greater than n
    public static int findNextPrime(int n)
    {
        // Note that difference (or m)
        // should be greater than 1.
        int nextPrime = n + 2;
     
        // loop continuously until
        // isPrime returns true
        // for a number above n
        while (true)
        {
     
            // Ignoring the prime number
            // that is 1 greater than n
            if (isPrime(nextPrime) == true)
                break;
     
            nextPrime++;
    }
 
    return nextPrime;
    }
     
    // Returns n-th
    // Fortunate number
    public static int fortunateNumber(int n)
    {
        int p = primorial(n);
        return findNextPrime(p) - p;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 5;
        Console.WriteLine(fortunateNumber(n));
    }
}
 
// This code is contributed
// by anuj_67.


PHP


Javascript


输出:
23

优化:可以使用Eratosthenes筛网优化上述解决方案。