首页  >  文章  >  后端开发  >  找到第n个幸运数

找到第n个幸运数

WBOY
WBOY转载
2023-09-11 19:09:111117浏览

找到第n个幸运数

幸运数字 - 它是 m > 1 的最小整数,对于给定的正整数 n,pn# + m 是素数,其中 pn# 是第一个 n 的乘积质数。

例如,要计算第三个幸运数字,首先计算前 3 个素数 (2, 3, 5) 的乘积,即 30。加 2 后得到 32,这是偶数,加 3 得到 33,是 3 的倍数。同样可以排除 6 以内的整数。加上 7 得到 37,这是一个素数。因此,7是第三个幸运数字。

第一个原初数的幸运数字是 -

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

问题陈述

给定一个数字n。找到第 n 个幸运数字。

示例 1

Input: n = 3
Output: 7

解释 - 前 3 个价格数字的乘积 -

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

示例 2

Input: n = 7
Output: 19

解释 - 前 7 个素数的乘积 -

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

方法一:原始方法

解决该问题的一个简单方法是首先计算 pn#,即前 n 个素数的乘积,然后找到 pn# 与下一个素数之间的差。获得的差额将是一个幸运的数字。

伪代码

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

示例:C++ 实现

在下面的程序中,通过计算前n个素数的本初以及找到本初后的下一个素数来计算幸运数。幸运数是下一个素数与本初数之间的差。

#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;
}

输出

15th Fortunate number : 107

时间复杂度 - O(nsqrt(n)),其中 prime() 函数的复杂度为 O(sqrt(n)),nthFortunate() 中的 for 循环的复杂度为 O(nsqrt(n))。

空间复杂度 - O(1)

方法 2:埃拉托斯特尼筛法

埃拉托色尼筛用于将所有质数达到一个极限,我们将给出一个值 MAX。在这种方法中,我们创建一个包含所有 true 条目的布尔数组,并将所有非素数索引标记为 false。然后将数组中的前 n 个素数相乘,得到前 n 个素数的乘积。然后与之前的方法类似,从 2 开始将乘积加 1,以获得下一个素数。下一个素数与乘积之差就是所需的幸运数。

伪代码

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

示例:C++ 实现

在下面的程序中,大小为 MAX 的布尔素数数组记录了 MAX 之前的所有素数。然后通过将前 n 个素数相乘来找到原初。然后与之前的方法类似,找到nextPrime。 nextPrime 和 Product 的区别在于幸运数字。

#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;
}

输出

15th Fortunate number : 107

时间复杂度 - O(n log(log(n)))

空间复杂度 - O(MAX)

结论

综上所述,第n个幸运数可以通过以下两种方式找到。

初等方法:求前n个素数的乘积,并根据乘积计算下一个素数。质数与乘积之差是第 n 个幸运数。

埃拉托斯特尼筛法:找出所有达到某个极限的素数,然后计算与下一个素数的乘积,从而找到幸运数。

仅由于变量大小的限制,这两种方法对于较小的 n 值都是有效的。对于更大的值,需要更高效和优化的解决方案。

以上是找到第n个幸运数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除