Check whether a number can be expressed as a product of single digit numbers

16 Apr, 2021
Given a non-negative number n. The problem is to check whether the given number n can be expressed as a product of single digit numbers or not.Examples:  
Input : n = 24
Output : Yes
Different combinations are:
(8*3) and (6*4)

Input : 68
Output : No
To represent 68 as product of 
number, 17 must be included which
is a two digit number.
 
Approach: We have to check whether the number n has no prime factors other than 2, 3, 5, 7. For this we repeatedly divide the number n by (2, 3, 5, 7) until it cannot be further divided by these numbers. After this process if n == 1, then it can be expressed as a product of single digit numbers, else if it is greater than 1, then it cannot be expressed. 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to check whether a number can be// expressed as a product of single digit numbers#include <bits/stdc++.h>using namespace std; // Number of single digit prime numbers#define SIZE 4 // function to check whether a number can be// expressed as a product of single digit numbersbool productOfSingelDgt(int n){    // if 'n' is a single digit number, then    // it can be expressed    if (n >= 0 && n <= 9)        return true;     // define single digit prime numbers array    int prime[] = { 2, 3, 5, 7 };     // repeatedly divide 'n' by the given prime    // numbers until all the numbers are used    // or 'n' > 1    for (int i = 0; i < SIZE && n > 1; i++)        while (n % prime[i] == 0)            n = n / prime[i];     // if true, then 'n' can    // be expressed    return (n == 1);} // Driver program to test aboveint main(){    int n = 24;    productOfSingelDgt(n)? cout << "Yes" :                           cout << "No";    return 0;}
// Java implementation to check whether// a number can be expressed as a// product of single digit numbersimport java.util.*; class GFG{ // Number of single digit prime numbersstatic int SIZE = 4; // function to check whether a number can// be expressed as a product of single// digit numbersstatic boolean productOfSingelDgt(int n){    // if 'n' is a single digit number,    // then it can be expressed    if (n >= 0 && n <= 9)        return true;     // define single digit prime numbers    // array    int[] prime = { 2, 3, 5, 7 };     // repeatedly divide 'n' by the given    // prime numbers until all the numbers    // are used or 'n' > 1    for (int i = 0; i < SIZE && n > 1; i++)        while (n % prime[i] == 0)            n = n / prime[i];     // if true, then 'n' can    // be expressed    return (n == 1);} // Driver program to test abovepublic static void main (String[] args){    int n = 24;    if(productOfSingelDgt(n))    System.out.println("Yes");    else    System.out.println("No");}     }/* This code is contributed by Mr. Somesh Awasthi */
# Python3 program to check# whether a number can be# expressed as a product of# single digit numbers # Number of single digit# prime numbersSIZE = 4 # function to check whether# a number can be# expressed as a product# of single digit numbersdef productOfSingelDgt(n):     # if 'n' is a single digit        # number, then    # it can be expressed    if n >= 0 and n <= 9:        return True     # define single digit prime        # numbers array    prime = [ 2, 3, 5, 7 ]     # repeatedly divide 'n' by        # the given prime    # numbers until all the        # numbers are used    # or 'n' > 1    i = 0    while i < SIZE and n > 1:        while n % prime[i] == 0:            n = n / prime[i]        i += 1     # if true, then 'n' can    # be expressed    return n == 1 n = 24if productOfSingelDgt(n):    print ("Yes")else :    print ("No") # This code is contributed# by Shreyanshi Arun.
// C# implementation to check whether// a number can be expressed as a// product of single digit numbersusing System; class GFG {     // Number of single digit prime numbers    static int SIZE = 4;     // function to check whether a number can    // be expressed as a product of single    // digit numbers    static bool productOfSingelDgt(int n)    {        // if 'n' is a single digit number,        // then it can be expressed        if (n >= 0 && n <= 9)            return true;         // define single digit prime numbers        // array        int[] prime = { 2, 3, 5, 7 };         // repeatedly divide 'n' by the given        // prime numbers until all the numbers        // are used or 'n' > 1        for (int i = 0; i < SIZE && n > 1; i++)            while (n % prime[i] == 0)                n = n / prime[i];         // if true, then 'n' can        // be expressed        return (n == 1);    }     // Driver program to test above    public static void Main()    {        int n = 24;        if (productOfSingelDgt(n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    } } // This code is contributed by Sam007
<?php// PHP implementation to check// whether a number can be// expressed as a product of// single digit numbers // function to check whether// a number can be expressed//as a product of single// digit numbersfunction productOfSingelDgt($n,$SIZE){         // if 'n' is a single    // digit number, then    // it can be expressed    if ($n >= 0 && $n <= 9)        return true;     // define single digit    // prime numbers array    $prime = array(2, 3, 5, 7);     // repeatedly divide 'n'    // by the given prime    // numbers until all    // the numbers are used    // or 'n' > 1    for ($i = 0; $i < $SIZE && $n > 1; $i++)        while ($n % $prime[$i] == 0)            $n = $n / $prime[$i];     // if true, then 'n' can    // be expressed    return ($n == 1);}     // Driver Code    $SIZE = 4;    $n = 24;    if(productOfSingelDgt($n, $SIZE))        echo "Yes" ;    else        echo "No"; // This code is contributed by Sam007?>
<script>// javascript implementation to check whether// a number can be expressed as a// product of single digit numbers     // Number of single digit prime numbers    const SIZE = 4;     // function to check whether a number can    // be expressed as a product of single    // digit numbers    function productOfSingelDgt(n)    {             // if 'n' is a single digit number,        // then it can be expressed        if (n >= 0 && n <= 9)            return true;         // define single digit prime numbers        // array        var prime = [ 2, 3, 5, 7 ];         // repeatedly divide 'n' by the given        // prime numbers until all the numbers        // are used or 'n' > 1        for (i = 0; i < SIZE && n > 1; i++)            while (n % prime[i] == 0)                n = n / prime[i];         // if true, then 'n' can        // be expressed        return (n == 1);    }     // Driver program to test above        var n = 24;        if (productOfSingelDgt(n))            document.write("Yes");        else            document.write("No"); // This code is contributed by gauravrajput1</script>
Output:  
Yes
Time Complexity: O(num), where num is the number of prime factors (2, 3, 5, 7) of n.This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
GauravRajput1
Prime Number
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.