📜  前N个质数的总和

📅  最后修改于: 2021-06-26 20:03:33             🧑  作者: Mango

给定一个整数“ n”,任务是找到第一个“ n”素数之和。

例子:

Input: N = 4
Output: 17
2, 3, 5, 7 are first 4 prime numbers so their sum is equal to 17

Input: N = 40
Output: 3087

方法:

  • 创建一个筛子,这将帮助我们确定数字在O(1)时间内是否为质数。
  • 运行一个从1开始的循环,直到,除非我们找到n个质数。
  • 将所有质数相加,而忽略非质数。
  • 然后,显示第1个N质数的总和。

下面是上述解决方案的实现

C++
// C++ implementation of above solution
#include 
using namespace std;
#define MAX 10000
 
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
    memset(prime, true, sizeof(prime));
 
    prime[1] = false;
 
    for (int p = 2; p * p <= MAX; p++) {
 
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) {
 
            // Set all multiples of p to non-prime
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// find the sum of 1st N prime numbers
int solve(int n)
{
    // count of prime numbers
    int count = 0, num = 1;
 
    // sum of prime numbers
    long long int sum = 0;
 
    while (count < n) {
 
        // if the number is prime add it
        if (prime[num]) {
            sum += num;
 
            // increase the count
            count++;
        }
 
        // get to next number
        num++;
    }
    return sum;
}
 
// Driver code
int main()
{
    // create the sieve
    SieveOfEratosthenes();
 
    int n = 4;
 
    // find the value of 1st n prime numbers
    cout << "Sum of 1st N prime numbers are :" << solve(n);
 
    return 0;
}


Java
// Java implementation of above solution
public class Improve {
     
    final static double MAX = 10000 ;
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    static boolean prime[] = new boolean [(int) (MAX + 1.0)] ;
    static void SieveOfEratosthenes()
    {
          
        for(int i = 0; i <= MAX; i++)
            prime[i] = true ;
         
        prime[1] = false;
       
        for (int p = 2; p * p <= MAX; p++) {
       
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
       
                // Set all multiples of p to non-prime
                for (int i = p * 2; i <= MAX; i += p)
                    prime[i] = false;
            }
        }
    }
       
    // find the sum of 1st N prime numbers
    static int solve(int n)
    {
        // count of prime numbers
        int count = 0, num = 1;
       
        // sum of prime numbers
        long sum = 0;
       
        while (count < n) {
       
            // if the number is prime add it
            if (prime[num]) {
                sum += num;
       
                // increase the count
                count++;
            }
       
            // get to next number
            num++;
        }
        return (int) sum;
    }
    // Driver code 
    public static void main(String args[])
    {
        // create the sieve
        SieveOfEratosthenes();
       
        int n = 4;
       
        // find the value of 1st n prime numbers
        System.out.println("Sum of 1st N prime numbers are :" + solve(n));
           
    }
    // This Code is contributed by ANKITRAI1
}


Python 3
# Python3 implementation of
# above solution
MAX = 10000
 
# Create a boolean array "prime[0..n]"
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is Not a prime, else true.
prime = [True for i in range(MAX + 1)]
def SieveOfEratosthenes():
 
    prime[1] = False
 
    for p in range(2, MAX + 1):
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
             
            # Set all multiples of
            # p to non-prime
            i = p * 2
            while(i <= MAX):
                prime[i] = False
                i = i + p
         
# find the sum of 1st
# N prime numbers
def solve( n):
 
    # count of prime numbers
    count = 0
    num = 1
 
    # sum of prime numbers
    total = 0
 
    while (count < n):
 
        # if the number is prime add it
        if ( prime[num] ):
            total = total + num
 
            # increase the count
            count = count + 1
         
        # get to next number
        num = num + 1
     
    return total
 
# Driver code
# create the sieve
SieveOfEratosthenes()
 
n = 4
 
# find the value of 1st
# n prime numbers
print("Sum of 1st N prime " +
      "numbers are :", solve(n))
 
# This code is contributed by ash264


C#
//C# implementation of above solution
 
using System;
 
public class GFG{
     
     static double MAX = 10000 ;
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    static bool []prime = new bool [(int)(MAX + 1.0)] ;
    static void SieveOfEratosthenes()
    {
         
        for(int i = 0; i <= MAX; i++)
            prime[i] = true ;
         
        prime[1] = false;
         
        for (int p = 2; p * p <= MAX; p++) {
         
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
         
                // Set all multiples of p to non-prime
                for (int i = p * 2; i <= MAX; i += p)
                    prime[i] = false;
            }
        }
    }
         
    // find the sum of 1st N prime numbers
    static int solve(int n)
    {
        // count of prime numbers
        int count = 0, num = 1;
         
        // sum of prime numbers
        long sum = 0;
         
        while (count < n) {
         
            // if the number is prime add it
            if (prime[num]) {
                sum += num;
         
                // increase the count
                count++;
            }
         
            // get to next number
            num++;
        }
        return (int) sum;
    }
    // Driver code
    static public void Main (){
        // create the sieve
        SieveOfEratosthenes();
        int n = 4;
        // find the value of 1st n prime numbers
        Console.WriteLine("Sum of 1st N prime numbers are :" + solve(n));
             
    }
// This Code is contributed by ajit.
}


Javascript


输出:
Sum of 1st N prime numbers are :17

注意(对于竞争性编程):在包含大量查询的问题中,可以使用向量存储10 ^ 8范围内的所有素数,这将占用额外的O(N)空间。我们还可以使用前缀数组存储前N个质数的总和,范围为10 ^ 8。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。