Given an integer N, the task is to print all the multiplicative primes ≤ N.
Multiplicative Primes are the primes such that the product of their digits is also a prime. For example; 2, 3, 7, 13, 17, …
Examples:
Input: N = 10
Output: 2 3 5 7Input: N = 3
Output: 2 3
Approach: Using Sieve of Eratosthenes check for all the primes ≤ N whether they are multiplicative primes i.e. product of their digits is also a prime. If yes then print those multiplicative primes.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the digit product of n int digitProduct(int n) { int prod = 1; while (n) { prod = prod * (n % 10); n = n / 10; } return prod; } // Function to print all multiplicative primes <= n void printMultiplicativePrimes(int n) { // Create a boolean array "prime[0..n+1]". A // value in prime[i] will finally be false // if i is Not a prime, else true. bool prime[n + 1]; memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { // If i is prime and its digit sum is also prime // i.e. i is a multiplicative prime if (prime[i] && prime[digitProduct(i)]) cout << i << " "; } } // Driver code int main() { int n = 10; printMultiplicativePrimes(n); } |
PHP
<?php // PHP implementation of the approach // Function to return the digit product of n function digitProduct($n) { $prod = 1; while ($n) { $prod = $prod * ($n % 10); $n = floor($n / 10); } return $prod; } // Function to print all multiplicative // primes <= n function printMultiplicativePrimes($n) { // Create a boolean array "prime[0..n+1]". // A value in prime[i] will finally be // false if i is Not a prime, else true. $prime = array_fill(0, $n + 1, true); $prime[0] = $prime[1] = false; for ($p = 2; $p * $p <= $n; $p++) { // If prime[p] is not changed, then // it is a prime if ($prime[$p]) { // Update all multiples of p for ($i = $p * 2; $i <= $n; $i += $p) $prime[$i] = false; } } for ($i = 2; $i <= $n; $i++) { // If i is prime and its digit sum is also // prime i.e. i is a multiplicative prime if ($prime[$i] && $prime[digitProduct($i)]) echo $i, " "; } } // Driver code $n = 10; printMultiplicativePrimes($n); // This code is contributed by Ryuga. ?> |
2 3 5 7
Recommended Posts:
- Print all Proth primes up to N
- Print all safe primes below N
- Segmented Sieve (Print Primes in a Range)
- Sieve of Sundaram to print all primes smaller than n
- Length of largest sub-array having primes strictly greater than non-primes
- Multiplicative order
- Modular multiplicative inverse
- Modular multiplicative inverse from 1 to n
- Palindromic Primes
- Circular primes less than n
- Product of all primes in the range from L to R
- Alternate Primes till N
- Count Primes in Ranges
- Number of primes in a subarray (with updates)
- Check if a number is sandwiched between primes
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.
Improved By : Ryuga



