📜  在第K个数组中找到质数之和

📅  最后修改于: 2021-04-22 02:44:25             🧑  作者: Mango

给定K个数组,其中第一个数组包含第一个质数,第二个数组包含接下来的2个质数,第三个数组包含接下来的3个质数,依此类推。任务是在第K数组中找到素数之和。

例子:

方法:可以使用Eratosthenes筛子来查找直至所需元素的所有素数。从1K – 1的数组中素数的计数将为cnt = 1 + 2 + 3 +…+(K – 1)=(K *(K – 1))/ 2 。现在,从筛子阵列的(cnt + 1)素数开始,开始加所有素数,直到精确地添加了K个素数,然后打印总和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 1000000
  
// To store whether a number is prime or not
bool prime[MAX];
  
// Function for Sieve of Eratosthenes
void SieveOfEratosthenes()
{
    // 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.
    for (int i = 0; i < MAX; i++)
        prime[i] = true;
  
    for (int p = 2; p * p < MAX; p++) {
  
        // If prime[p] is not changed then it is a prime
        if (prime[p]) {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i < MAX; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// primes in the Kth array
int sumPrime(int k)
{
  
    // Update vector v to store all the
    // prime numbers upto MAX
    SieveOfEratosthenes();
    vector v;
    for (int i = 2; i < MAX; i++) {
        if (prime[i])
            v.push_back(i);
    }
  
    // To store the sum of primes
    // in the kth array
    int sum = 0;
  
    // Count of primes which are in
    // the arrays from 1 to k - 1
    int skip = (k * (k - 1)) / 2;
  
    // k is the number of primes
    // in the kth array
    while (k > 0) {
        sum += v[skip];
        skip++;
  
        // A prime has been
        // added to the sum
        k--;
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int k = 3;
  
    cout << sumPrime(k);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
static int MAX = 1000000;
  
// To store whether a number is prime or not
static boolean []prime = new boolean[MAX];
  
// Function for Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
    // 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.
    for (int i = 0; i < MAX; i++)
        prime[i] = true;
  
    for (int p = 2; p * p < MAX; p++) 
    {
  
        // If prime[p] is not changed
        // then it is a prime
        if (prime[p]) 
        {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i < MAX; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// primes in the Kth array
static int sumPrime(int k)
{
  
    // Update vector v to store all the
    // prime numbers upto MAX
    SieveOfEratosthenes();
    Vector v = new Vector<>();
    for (int i = 2; i < MAX; i++) 
    {
        if (prime[i])
            v.add(i);
    }
  
    // To store the sum of primes
    // in the kth array
    int sum = 0;
  
    // Count of primes which are in
    // the arrays from 1 to k - 1
    int skip = (k * (k - 1)) / 2;
  
    // k is the number of primes
    // in the kth array
    while (k > 0)
    {
        sum += v.get(skip);
        skip++;
  
        // A prime has been
        // added to the sum
        k--;
    }
  
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int k = 3;
  
    System.out.println(sumPrime(k));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
from math import sqrt
  
MAX = 1000000
  
# 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] * MAX
  
# Function for Sieve of Eratosthenes 
def SieveOfEratosthenes() :
  
    for p in range(2, int(sqrt(MAX)) + 1) : 
  
        # If prime[p] is not changed
        # then it is a prime 
        if (prime[p]) :
  
            # Update all multiples of p greater than or 
            # equal to the square of it 
            # numbers which are multiple of p and are 
            # less than p^2 are already been marked. 
            for i in range(p * p, MAX, p) :
                prime[i] = False; 
  
# Function to return the sum of 
# primes in the Kth array 
def sumPrime(k) : 
  
    # Update vector v to store all the 
    # prime numbers upto MAX 
    SieveOfEratosthenes(); 
    v = []; 
    for i in range(2, MAX) :
        if (prime[i]) :
            v.append(i); 
  
    # To store the sum of primes 
    # in the kth array 
    sum = 0; 
  
    # Count of primes which are in 
    # the arrays from 1 to k - 1 
    skip = (k * (k - 1)) // 2; 
  
    # k is the number of primes 
    # in the kth array 
    while (k > 0) :
        sum += v[skip]; 
        skip += 1; 
  
        # A prime has been 
        # added to the sum 
        k -= 1; 
  
    return sum; 
  
# Driver code 
if __name__ == "__main__" :
      
    k = 3;
      
    print(sumPrime(k)); 
  
# This code is contributed by AnkitRai01


C#
// C# mplementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
static int MAX = 1000000;
  
// To store whether a number is prime or not
static bool []prime = new bool[MAX];
  
// Function for Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
    // 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.
    for (int i = 0; i < MAX; i++)
        prime[i] = true;
  
    for (int p = 2; p * p < MAX; p++) 
    {
  
        // If prime[p] is not changed
        // then it is a prime
        if (prime[p]) 
        {
  
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i < MAX; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to return the sum of
// primes in the Kth array
static int sumPrime(int k)
{
  
    // Update vector v to store all the
    // prime numbers upto MAX
    SieveOfEratosthenes();
    List v = new List();
    for (int i = 2; i < MAX; i++) 
    {
        if (prime[i])
            v.Add(i);
    }
  
    // To store the sum of primes
    // in the kth array
    int sum = 0;
  
    // Count of primes which are in
    // the arrays from 1 to k - 1
    int skip = (k * (k - 1)) / 2;
  
    // k is the number of primes
    // in the kth array
    while (k > 0)
    {
        sum += v[skip];
        skip++;
  
        // A prime has been
        // added to the sum
        k--;
    }
  
    return sum;
}
  
// Driver code
public static void Main(String[] args)
{
    int k = 3;
  
    Console.WriteLine(sumPrime(k));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
31