Check whether a number can be expressed as a product of single digit numbers
Last Updated :
03 Oct, 2023
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++
// 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 numbers
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
int main()
{
int n = 24;
productOfSingelDgt(n)? cout << "Yes" :
cout << "No";
return 0;
}
Java
// Java implementation to check whether
// a number can be expressed as a
// product of single digit numbers
import java.util.*;
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 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 above
public 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
# Python3 program to check
# whether a number can be
# expressed as a product of
# single digit numbers
# Number of single digit
# prime numbers
SIZE = 4
# function to check whether
# a number can be
# expressed as a product
# of single digit numbers
def 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 = 24
if productOfSingelDgt(n):
print ("Yes")
else :
print ("No")
# This code is contributed
# by Shreyanshi Arun.
C#
// C# implementation to check whether
// a number can be expressed as a
// product of single digit numbers
using 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
JavaScript
<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>
PHP
<?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 numbers
function 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
?>
Output:
Yes
Time Complexity: O(num), where num is the number of prime factors (2, 3, 5, 7) of n.
Auxiliary space: O(1) as it is using constant space
Similar Reads
Check if a number can be represented as the sum of numbers with at least one digit equal to K Given integers N and K, the task is to check if a number can be represented as the sum of numbers that have at least one digit equal to K. Example: Input: N = 68, K = 7Output: YESExplanation: 68 = (27 + 17 + 17 + 7). Each number has atleast one digit equal to 7. Input: N = 23, K = 3Output: YESExplan
6 min read
Sum and Product of digits in a number that divide the number Given a positive integer N. The task is to find the sum and product of digits of the number which evenly divides the number n. Examples: Input: N = 12 Output: Sum = 3, product = 2 1 and 2 divide 12. So, their sum is 3 and product is 2. Input: N = 1012 Output: Sum = 4, product = 2 1, 1 and 2 divide 1
5 min read
Check if the product of digit sum and its reverse equals the number or not Given a number, check whether the product of digit sum and reverse of digit sum equals the number or not.Examples: Input : 1729 Output : Yes Explanation: digit sum = 1 + 7 + 2 + 9 = 19 Reverse of digit sum = 91 Product = digit sum * Reverse of digit sum = 19 * 91 = 1729 Input : 2334 Output : No Flow
6 min read
Check whether product of digits at even places of a number is divisible by K Given a number N, the task is to check whether the product of digits at even places of a number is divisible by K. If it is divisible, output "YES" otherwise output "NO". Examples: Input: N = 5478, K = 5 Output: YES Since, 5 * 7 = 35, which is divisible by 5 Input: N = 19270, K = 2 Output: NO Approa
7 min read
Check whether a number is Tech Number or not Given a number, the task is to check if it is a Tech number or not. Note: A Tech number is a number that has an even number of digits and if the number is split into two equal halves(From the middle), then the square of the sum of these halves is equal to the number itself. Examples:Input: n = 81 Ou
6 min read
Cumulative product of digits of all numbers in the given range Given two integers L and R, the task is to find the cumulative product of digits (i.e. product of the product of digits) of all Natural numbers in the range L to R. Examples: Input: L = 2, R = 5 Output: 14 Explanation: 2 * 3 * 4 * 5 = 120Input: L = 11, R = 15 Output: 120 Explanation: (1*1) * (1*2) *
5 min read