📜  计算可以表示为连续素数之和的素数

📅  最后修改于: 2021-05-06 20:42:37             🧑  作者: Mango

给定整数N ,任务是找到最多N个可以表示为连续质数之和的质数数。

例子:

方法:想法是使用原始性测试算法。使用此方法,可以找到所有不超过N的素数。之后,可以找到每个可以表示为连续质数的数字。请按照以下步骤解决问题:

  1. 1N遍历每个数字,检查它是否为质数,并将其存储在向量中。
  2. 对向量中所有存储的质数进行排序。
  3. 令向量中存在X个质数。将sum初始化为找到的最小素数,即向量中索引0处的元素。
  4. [1,X – 1]范围内迭代,并将每个元素加到sum上
  5. 加完后,检查总和是否为素数,以及总和是否小于N。如果发现为真,则增加计数器。否则,如果总和大于N ,则中断循环。
  6. 完成上述所有步骤后,打印存储在计数器中的素数计数

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Function to check if a
// number is prime or not
int isprm(int n)
{
    // Base Case
    if (n <= 1)
        return 0;
    if (n <= 3)
        return 1;
    if (n % 2 == 0 || n % 3 == 0)
        return 0;
  
    // Iterate till [5, sqrt(N)] to
    // detect primality of numbers
    for (int i = 5;
         i * i <= n; i = i + 6) {
  
        // If N is divisible by i
        // or i + 2
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;
    }
  
    // Return 1 if N is prime
    return 1;
}
  
// Function to count the prime numbers
// which can be expressed as sum of 
// consecutive prime numbers
int countprime(int n)
{
    // Initialize count as 0
    int count = 0;
  
    // Stores prime numbers
    vector primevector;
  
    for (int i = 2; i <= n; i++) {
  
        // If i is prime
        if (isprm(i) == 1) {
            primevector.push_back(i);
        }
    }
  
    // Initialize the sum
    int sum = primevector[0];
  
    // Find all required primes upto N
    for (int i = 1;
         i < primevector.size(); i++) {
  
        // Add it to the sum
        sum += primevector[i];
        if (sum > n)
            break;
        if (isprm(sum) == 1) {
            count++;
        }
    }
  
    // Return the final count
    return count;
}
  
// Driver Code
int main()
{
    // Given number N
    int N = 45;
  
    // Function Call
    cout << countprime(N);
  
    return 0;
}


Java
// Java program for 
// the above approach
import java.util.*;
class GFG{
  
// Function to check if a
// number is prime or not
static int isprm(int n)
{
  // Base Case
  if (n <= 1)
    return  0;
  if (n <= 3)
    return 1;
  if (n % 2 == 0 || 
      n % 3 == 0)
    return 0;
  
  // Iterate till [5, Math.sqrt(N)]
  // to detect primality of numbers
  for (int i = 5; i * i <= n; 
           i = i + 6) 
  {
    // If N is divisible by i
    // or i + 2
    if (n % i == 0 || 
        n % (i + 2) == 0)
      return 0;
  }
  
  // Return 1 if N is prime
  return 1;
}
  
// Function to count the prime numbers
// which can be expressed as sum of 
// consecutive prime numbers
static int countprime(int n)
{
  // Initialize count as 0
  int count = 0;
  
  // Stores prime numbers
  Vector primevector = 
                  new Vector<>();
  
  for (int i = 2; i <= n; i++) 
  {
    // If i is prime
    if (isprm(i) == 1) 
    {
      primevector.add(i);
    }
  }
  
  // Initialize the sum
  int sum = primevector.elementAt(0);
  
  // Find all required primes upto N
  for (int i = 1; 
           i < primevector.size(); i++) 
  {
    // Add it to the sum
    sum += primevector.elementAt(i);
    if (sum > n)
      break;
    if (isprm(sum) == 1) 
    {
      count++;
    }
  }
  
  // Return the final count
  return count;
}
  
// Driver Code
public static void main(String[] args)
{
  // Given number N
  int N = 45;
  
  // Function Call
  System.out.print(countprime(N));
}
}
  
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
  
# Function to check if a
# number is prime or not
def isprm(n):
      
    # Base Case
    if (n <= 1):
        return 0
    if (n <= 3):
        return 1
    if (n % 2 == 0 or n % 3 == 0):
        return 0
  
    # Iterate till [5, sqrt(N)] to
    # detect primality of numbers
    i = 5
    while (i * i <= n):
  
        # If N is divisible by i
        # or i + 2
        if (n % i == 0 or 
            n % (i + 2) == 0):
            return 0
          
        i = i + 6
      
    # Return 1 if N is prime
    return 1
  
# Function to count the prime numbers
# which can be expressed as sum of 
# consecutive prime numbers
def countprime(n):
      
    # Initialize count as 0
    count = 0
  
    # Stores prime numbers
    primevector = []
  
    for i in range(2, n + 1):
  
        # If i is prime
        if (isprm(i) == 1):
            primevector.append(i)
          
    # Initialize the sum
    sum = primevector[0]
  
    # Find all required primes upto N
    for i in range(1, len(primevector)):
  
        # Add it to the sum
        sum += primevector[i]
        if (sum > n):
            break
        if (isprm(sum) == 1):
            count += 1
  
    # Return the final count
    return count
  
# Driver Code
  
# Given number N
N = 45
  
# Function call
print(countprime(N))
  
# This code is contributed by code_hunt


C#
// C# program for 
// the above approach
using System;
using System.Collections.Generic;
class GFG{
  
// Function to check if a
// number is prime or not
static int isprm(int n)
{
  // Base Case
  if (n <= 1)
    return  0;
  if (n <= 3)
    return 1;
  if (n % 2 == 0 || 
      n % 3 == 0)
    return 0;
  
  // Iterate till [5, Math.Sqrt(N)]
  // to detect primality of numbers
  for (int i = 5; i * i <= n; 
           i = i + 6) 
  {
    // If N is divisible by i
    // or i + 2
    if (n % i == 0 || 
        n % (i + 2) == 0)
      return 0;
  }
  
  // Return 1 if N is prime
  return 1;
}
  
// Function to count the prime numbers
// which can be expressed as sum of 
// consecutive prime numbers
static int countprime(int n)
{
  // Initialize count as 0
  int count = 0;
  
  // Stores prime numbers
  List primevector = new List();
  
  for (int i = 2; i <= n; i++) 
  {
    // If i is prime
    if (isprm(i) == 1) 
    {
      primevector.Add(i);
    }
  }
  
  // Initialize the sum
  int sum = primevector[0];
  
  // Find all required primes upto N
  for (int i = 1; i < primevector.Count; i++) 
  {
    // Add it to the sum
    sum += primevector[i];
    if (sum > n)
      break;
    if (isprm(sum) == 1) 
    {
      count++;
    }
  }
  
  // Return the readonly count
  return count;
}
  
// Driver Code
public static void Main(String[] args)
{
  // Given number N
  int N = 45;
  
  // Function Call
  Console.Write(countprime(N));
}
}
  
// This code is contributed by shikhasingrajput


输出:
3

时间复杂度: O(N 3/2 )
辅助空间: O(√N)

高效方法:可以通过使用Eratosthenes筛子预先计算最高达N的素数来优化上述方法。

时间复杂度: O(N * log(logN))
辅助空间: O(log(logN))