📜  没有奇数质数的质数之和

📅  最后修改于: 2021-04-22 03:08:50             🧑  作者: Mango

给定整数N。任务是找到不包含任何奇数质数作为其数字的前N个质数的总和。

其中一些素数是2、11、19、29、41……

例子:

方法 :

  • 我们首先使用Eratosthenes筛子来存储所有素数。
  • 下一步检查每个素数是否存在奇数素数。
  • 如果没有这样的数字,那么我们会将这个质数包含在我们所需的答案中
  • 继续以上步骤,直到得到N个素数

下面是上述方法的实现:

C++
#include 
using namespace std;
  
#define MAX 100005
  
// Find all prime numbers
vector addPrimes()
{
    int n = MAX;
  
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p <= n; p++) {
  
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    vector ans;
    // Store all prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            ans.push_back(p);
  
    return ans;
}
  
// Function to check if a digit is odd prime or not
bool is_prime(int n) 
{
    return (n == 3 || n == 5 || n == 7);
}
  
// Function to find sum
int find_Sum(int n) 
{
    // To store required answer
    int sum = 0;
      
    // Get all prime numbers
    vector v = addPrimes();
      
    // Traverse through all the prime numbers
    for (int i = 0; i < v.size() and n; i++) 
    {
        // Flag stores 1 if a number does 
        // not contain any odd primes
        int flag = 1;
        int a = v[i];
          
        // Find all digits of a number
        while (a != 0)
        {
            int d = a % 10;
            a = a / 10;
            if (is_prime(d)) {
                flag = 0;
                break;
            }
        }
          
        // If number does not contain any odd primes
        if (flag==1) 
        {
            n--;
            sum = sum + v[i];
        }
    }
  
    // Return the required answer
    return sum;
}
  
// Driver code
int main()
{
    int n = 7;
      
    // Function call
    cout << find_Sum(n); 
  
    return 0;
}


Java
// Java program for above approach
import java.util.*;
  
class GFG 
{
static int MAX = 100005;
  
// Find all prime numbers
static Vector addPrimes()
{
    int n = MAX;
  
    boolean []prime = new boolean[n + 1];
    Arrays.fill(prime, true);
  
    for (int p = 2; p * p <= n; p++) 
    {
        if (prime[p] == true) 
        {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    Vector ans = new Vector();
      
    // Store all prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            ans.add(p);
  
    return ans;
}
  
// Function to check if a digit
// is odd prime or not
static boolean is_prime(int n) 
{
    return (n == 3 || n == 5 || n == 7);
}
  
// Function to find sum
static int find_Sum(int n) 
{
    // To store required answer
    int sum = 0;
      
    // Get all prime numbers
    Vector v = addPrimes();
      
    // Traverse through all the prime numbers
    for (int i = 0; i < v.size() && n > 0; i++) 
    {
        // Flag stores 1 if a number does 
        // not contain any odd primes
        int flag = 1;
        int a = v.get(i);
          
        // Find all digits of a number
        while (a != 0)
        {
            int d = a % 10;
            a = a / 10;
            if (is_prime(d))
            {
                flag = 0;
                break;
            }
        }
          
        // If number does not contain
        // any odd primes
        if (flag == 1) 
        {
            n--;
            sum = sum + v.get(i);
        }
    }
  
    // Return the required answer
    return sum;
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 7;
      
    // Function call
    System.out.println(find_Sum(n)); 
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program for above approach
MAX = 100005
  
def addPrimes():
    n = MAX
  
    prime = [True for i in range(n + 1)]
  
    for p in range(2, n + 1):
  
        if p * p > n:
            break
  
        if (prime[p] == True):
            for i in range(2 * p, n + 1, p):
                prime[i] = False
  
    ans = []
      
    # Store all prime numbers
    for p in range(2, n + 1):
        if (prime[p]):
            ans.append(p)
  
    return ans
  
# Function to check if 
# a digit is odd prime or not
def is_prime(n):
    if n in [3, 5, 7]:
        return True
    return False
  
# Function to find sum
def find_Sum(n):
      
    # To store required answer
    Sum = 0
  
    # Get all prime numbers
    v = addPrimes()
  
    # Traverse through all the prime numbers
    for i in range(len(v)):
          
        # Flag stores 1 if a number does
        # not contain any odd primes
        flag = 1
        a = v[i]
  
        # Find all digits of a number
        while (a != 0):
  
            d = a % 10;
            a = a // 10;
            if (is_prime(d)):
                flag = 0
                break
  
        # If number does not contain any odd primes
        if (flag == 1):
            n -= 1
            Sum = Sum + v[i]
        if n == 0:
            break
  
    # Return the required answer
    return Sum
  
# Driver code
n = 7
  
# Function call
print(find_Sum(n))
  
# This code is contributed by Mohit Kumar


C#
// C# program for above approach
using System;
using System.Collections.Generic;
  
class GFG 
{
static int MAX = 100005;
  
// Find all prime numbers
static List addPrimes()
{
    int n = MAX;
  
    Boolean []prime = new Boolean[n + 1];
    for(int i = 0; i < n + 1; i++)
        prime[i]=true;
  
    for (int p = 2; p * p <= n; p++) 
    {
        if (prime[p] == true) 
        {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    List ans = new List();
      
    // Store all prime numbers
    for (int p = 2; p <= n; p++)
        if (prime[p])
            ans.Add(p);
  
    return ans;
}
  
// Function to check if a digit
// is odd prime or not
static Boolean is_prime(int n) 
{
    return (n == 3 ||
            n == 5 || n == 7);
}
  
// Function to find sum
static int find_Sum(int n) 
{
    // To store required answer
    int sum = 0;
      
    // Get all prime numbers
    List v = addPrimes();
      
    // Traverse through all the prime numbers
    for (int i = 0; 
             i < v.Count && n > 0; i++) 
    {
        // Flag stores 1 if a number does 
        // not contain any odd primes
        int flag = 1;
        int a = v[i];
          
        // Find all digits of a number
        while (a != 0)
        {
            int d = a % 10;
            a = a / 10;
            if (is_prime(d))
            {
                flag = 0;
                break;
            }
        }
          
        // If number does not contain
        // any odd primes
        if (flag == 1) 
        {
            n--;
            sum = sum + v[i];
        }
    }
  
    // Return the required answer
    return sum;
}
  
// Driver code
public static void Main(String[] args) 
{
    int n = 7;
      
    // Function call
    Console.WriteLine(find_Sum(n)); 
}
}
  
// This code is contributed by Princi Singh


输出 :

252