Print all multiplicative primes <= N
Last Updated :
26 Feb, 2023
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 7
Input: 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.
Algorithm:
Step 1: Define a function of the static type which will return the int value and take input as an integer number and return the product of its digits.
Step 2: Now Initialize the variable of in type called prod with the initial value 1.
Step 3: Start a while loop which will calculate the product of digits:
a. Multiply prod by the remainder of n divided by 10.
b. Divide n by 10 and update the value of n with the result.
c. Repeat until n becomes 0.
Step 4: Now return prod as the digit product of n
Step 5: Create a function called printMultiplicativePrimes that outputs all multiplicative primes up to n given an integer input of n.
Step 6: Set every value in the prime boolean array, which has a size of n+1, to true during initialization.
Step 7: primes [0] and [1] should be set to false because they are not primes.
Step 8: Get all primes up to the square root of n using a for loop and Update all multiples of p by setting prime[i] to false if prime[p] is true.
Step 9: Print all prime multiplicative numbers up to n using another for loop and Print I if both prime[i] and prime[digitProduct(i)] are true.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int digitProduct( int n)
{
int prod = 1;
while (n) {
prod = prod * (n % 10);
n = n / 10;
}
return prod;
}
void printMultiplicativePrimes( int n)
{
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]) {
for ( int i = p * 2; i <= n; i += p)
prime[i] = false ;
}
}
for ( int i = 2; i <= n; i++) {
if (prime[i] && prime[digitProduct(i)])
cout << i << " " ;
}
}
int main()
{
int n = 10;
printMultiplicativePrimes(n);
}
|
Java
import java.io.*;
class GFG
{
static int digitProduct( int n)
{
int prod = 1 ;
while (n > 0 )
{
prod = prod * (n % 10 );
n = n / 10 ;
}
return prod;
}
static void printMultiplicativePrimes( int n)
{
boolean prime[] = new boolean [n + 1 ];
for ( int i = 0 ; i <= n; i++)
prime[i] = true ;
prime[ 0 ] = prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= n; p++)
{
if (prime[p])
{
for ( int i = p * 2 ; i <= n; i += p)
prime[i] = false ;
}
}
for ( int i = 2 ; i <= n; i++)
{
if (prime[i] && prime[digitProduct(i)])
System.out.print( i + " " );
}
}
public static void main (String[] args)
{
int n = 10 ;
printMultiplicativePrimes(n);
}
}
|
Python3
from math import sqrt
def digitProduct(n):
prod = 1
while (n):
prod = prod * (n % 10 )
n = int (n / 10 )
return prod
def printMultiplicativePrimes(n):
prime = [ True for i in range (n + 1 )]
prime[ 0 ] = prime[ 1 ] = False
for p in range ( 2 , int (sqrt(n)) + 1 , 1 ):
if (prime[p]):
for i in range (p * 2 , n + 1 , p):
prime[i] = False
for i in range ( 2 , n + 1 , 1 ):
if (prime[i] and prime[digitProduct(i)]):
print (i, end = " " )
if __name__ = = '__main__' :
n = 10
printMultiplicativePrimes(n)
|
C#
class GFG
{
static int digitProduct( int n)
{
int prod = 1;
while (n > 0)
{
prod = prod * (n % 10);
n = n / 10;
}
return prod;
}
static void printMultiplicativePrimes( int n)
{
bool [] prime = new bool [n + 1 ];
for ( int i = 0; i <= n; i++)
prime[i] = true ;
prime[0] = prime[1] = false ;
for ( int p = 2; p * p <= n; p++)
{
if (prime[p])
{
for ( int i = p * 2; i <= n; i += p)
prime[i] = false ;
}
}
for ( int i = 2; i <= n; i++)
{
if (prime[i] && prime[digitProduct(i)])
System.Console.Write( i + " " );
}
}
static void Main()
{
int n = 10;
printMultiplicativePrimes(n);
}
}
|
PHP
<?php
function digitProduct( $n )
{
$prod = 1;
while ( $n )
{
$prod = $prod * ( $n % 10);
$n = floor ( $n / 10);
}
return $prod ;
}
function printMultiplicativePrimes( $n )
{
$prime = array_fill (0, $n + 1, true);
$prime [0] = $prime [1] = false;
for ( $p = 2; $p * $p <= $n ; $p ++)
{
if ( $prime [ $p ])
{
for ( $i = $p * 2; $i <= $n ; $i += $p )
$prime [ $i ] = false;
}
}
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $prime [ $i ] && $prime [digitProduct( $i )])
echo $i , " " ;
}
}
$n = 10;
printMultiplicativePrimes( $n );
?>
|
Javascript
<script>
function digitProduct(n)
{
let prod = 1;
while (n > 0)
{
prod = prod * (n % 10);
n = Math.floor(n / 10);
}
return prod;
}
function printMultiplicativePrimes(n)
{
let prime = new Array(n + 1);
for (let i = 0; i <= n; i++)
prime[i] = true ;
prime[0] = prime[1] = false ;
for (let p = 2; p * p <= n; p++)
{
if (prime[p])
{
for (let i = p * 2; i <= n; i += p)
prime[i] = false ;
}
}
for (let i = 2; i <= n; i++)
{
if (prime[i] && prime[digitProduct(i)])
document.write( i + " " );
}
}
let n = 10;
printMultiplicativePrimes(n);
</script>
|
Time Complexity: O(n3/2)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...