📜  大于等于给定数的连续质数。

📅  最后修改于: 2021-10-26 05:46:00             🧑  作者: Mango

题:

给定一个数 n,任务是找到两个连续的素数,使得这两个素数的乘积大于或等于 n。

例子:

方法:

假设 n 的范围是 10^8 到 10^10。我们无法使用筛法找出素数,因为范围高达 10^10。

我们可以通过以下方法找到所需的连续素数。

  • 找到小于 sqrt(n) 的最大素数并将其存储在临时变量中(第一个)。
  • 找到大于 sqrt(n) 的最小素数并将其存储在临时变量 (second) 中。
  • 如果第一个和第二个的乘积大于等于 n,则打印它。
  • 否则找到一个刚好大于秒的素数并将其与秒一起打印。

代码:

C++
//C++ program for the above approach
#include 
#define endl "\n"
#define ll long long
using namespace std;
 
//Function to check prime.
bool is_prime(ll n)
{
    if (n == 1)
    {
        return false;
    }
    for (ll i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
          // It means it is not
          // a prime
            return false;
        }
    }
  // No factor other than 1
  // therefore prime number
    return true;
}
 
//Function to find out the required
//consecutive primes.
void consecutive_primes(int n)
{
    ll first = -1, second = -1;
   
    //Finding first prime just
    // less than sqrt(n).
    for (ll i = sqrt(n); i >= 2; i--)
    {
        if (is_prime(i))
        {
            first = i;
            break;
        }
    }
    // Finding prime just greater
    //than sqrt(n).
    for (ll i = sqrt(n) + 1;
         i <= n / 2; i++)
    {
        if (is_prime(i))
        {
            second = i;
            break;
        }
    }
    // Product of both prime is greater
    // than n then print it
    if (first * second >= n)
    {
        cout << first << " "
          <


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to check prime.
static boolean is_prime(long n)
{
    if (n == 1)
    {
        return false;
    }
    for(long i = 2; i <= (long)Math.sqrt(n); i++)
    {
        if (n % i == 0)
        {
             
            // It means it is not
            // a prime
            return false;
        }
    }
    
    // No factor other than 1
    // therefore prime number
    return true;
}
 
// Function to find out the required
// consecutive primes.
static void consecutive_primes(long n)
{
    long first = -1, second = -1;
 
    // Finding first prime just
    // less than sqrt(n).
    for(long i = (long)Math.sqrt(n); i >= 2; i--)
    {
        if (is_prime(i))
        {
            first = i;
            break;
        }
    }
     
    // Finding prime just greater
    // than sqrt(n).
    for(long i = (long)Math.sqrt(n) + 1;
             i <= n / 2; i++)
    {
        if (is_prime(i))
        {
            second = i;
            break;
        }
    }
     
    // Product of both prime is greater
    // than n then print it
    if (first * second >= n)
    {
        System.out.println(first + " " + second);
    }
    
    // Finding prime greater than second
    else
    {
        for(long i = second + 1; i <= n; i++)
        {
            if (is_prime(i))
            {
                System.out.println(second + " " + i);
                return;
            }
        }
    }
}
 
// Driver code
public static void main(String args[])
{
    long n = 14;
    consecutive_primes(n);
}
}
 
// This code is contributed by splevel62


Python3
#python 3 program for the above approach
#Function to check prime.
from math import sqrt
 
def is_prime(n):
    if (n == 1):
        return False
    for i in range(2,int(sqrt(n))+1,1):
        if (n % i == 0):
            # It means it is not
            # a prime
            return False
  # No factor other than 1
  # therefore prime number
    return True
 
# Function to find out the required
# consecutive primes.
def consecutive_primes(n):
    first = -1
    second = -1
   
    # Finding first prime just
    # less than sqrt(n).
    i = int(sqrt(n))
    while(i >= 2):
        if (is_prime(i)):
            first = i
            break
        i -= 1
         
    # Finding prime just greater
    #than sqrt(n).
    for i in range(int(sqrt(n)) + 1,n//2 +1 ,1):
        if (is_prime(i)):
            second = i
            break
 
    # Product of both prime is greater
    # than n then print it
    if (first * second >= n):
        print(first,second)
         
    # Finding prime greater than second
    else:
        for i in range(second + 1,n+1,1):
            if (is_prime(i)):
                print(second,i)
                return
             
# Driver Program
if __name__ == '__main__':
    n = 14
    consecutive_primes(n)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to check prime.
    static bool is_prime(long n)
    {
        if (n == 1) {
            return false;
        }
        for (long i = 2; i <= (long)Math.Sqrt(n); i++) {
            if (n % i == 0)
            {
               
                // It means it is not
                // a prime
                return false;
            }
        }
       
        // No factor other than 1
        // therefore prime number
        return true;
    }
 
    // Function to find out the required
    // consecutive primes.
    static void consecutive_primes(long n)
    {
        long first = -1, second = -1;
 
        // Finding first prime just
        // less than sqrt(n).
        for (long i = (long)Math.Sqrt(n); i >= 2; i--) {
            if (is_prime(i)) {
                first = i;
                break;
            }
        }
        // Finding prime just greater
        // than sqrt(n).
        for (long i = (long)Math.Sqrt(n) + 1; i <= n / 2;
             i++) {
            if (is_prime(i)) {
                second = i;
                break;
            }
        }
        // Product of both prime is greater
        // than n then print it
        if (first * second >= n) {
            Console.WriteLine(first + " " + second);
        }
       
        // Finding prime greater than second
        else {
            for (long i = second + 1; i <= n; i++) {
                if (is_prime(i)) {
                    Console.WriteLine(second + " " + i);
                    return;
                }
            }
        }
    }
   
    // Driver Program
    public static void Main()
    {
        long n = 14;
        consecutive_primes(n);
    }
}
 
// This code is contributed  by ukasp.


Javascript


输出

3 5

时间复杂度:O(sqrt(n))

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程