Sum of prime numbers without odd prime digits

Given an integer N. The task is to find the sum of the first N prime numbers which don’t contain any odd primes as their digit.

Some of such prime numbers are 2, 11, 19, 29, 41 ……

Examples:

Input : N = 2
Output : 13
2 + 11 = 13

Input : N = 7
Output : 252

Approach :

  • We first use a Sieve of Eratosthenes to store all prime numbers.
  • Next check for each prime number if any odd prime digit is present or not.
  • If no such digit is present then we will include this prime to our required answer
  • Continue above step until we get N such prime numbers

Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
#define MAX 100005
  
// Find all prime numbers
vector<int> 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<int> 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<int> 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for above approach
import java.util.*;
  
class GFG 
{
static int MAX = 100005;
  
// Find all prime numbers
static Vector<Integer> 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<Integer> ans = new Vector<Integer>();
      
    // 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<Integer> 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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


Output :

252


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.