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 = 13Input : 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++
#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
filter_none
Java
// 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
filter_none
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 |
chevron_right
filter_none
C#
// C# program for above approach using System; using System.Collections.Generic; class GFG { static int MAX = 100005; // Find all prime numbers static List<int> 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<int> ans = new List<int>(); // 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<int> 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 |
chevron_right
filter_none
Output :
252
Recommended Posts:
- Print prime numbers with prime sum of digits in an array
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Find all the prime numbers of given number of digits
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Prime numbers after prime P with sum S
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Recursive sum of digits of a number is prime or not
- Largest number with prime digits
- Count occurrences of a prime number in the prime factorization of every element from the given range
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.



