Count all N digit numbers whose digits are multiple of X
Given two integers N and X, the task is to find the count of all possible N digit numbers whose every single digit is a multiple of X.
Examples :
Input: N = 1, X = 3
Output: 4
Explanation:
The single-digit numbers whose digits are multiple of 3 are 0, 3, 6 and 9.
So the answer will be 4.
Input: N = 2, X = 4
Output: 6
Explanation:
The two-digit numbers whose digits are multiple of 4 are 40, 44, 48, 80, 84, 88.
So the answer will be 6.
Naive Approach: The simplest approach is to iterate over all N-digit numbers, i.e. in the range [10N – 1, 10N – 1] and for each number, check if each digit of the number is a multiple of X or not. Increase the count of such numbers and print the final count.
Time Complexity: O((10N – 10N – 1)*N)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below in order to solve the problem:
- Count the total digits which are multiple of X, lets denote it as total_Multiples.
- BY arrange all the above digits in different positions to form an N-digit number the required count can be obtained.
Illustration:
For example N = 2, X = 3
Multiples of 3 are S = { 0, 3, 6, 9 }
- To find all 2-digit numbers, arrange all the multiples of X to form a 2-digit number and count it.
- Arrange the numbers from the set S. Taking 0 for the most significant bit will result in 1-digit number, so there are 3 ways to arrange the most significant position of a number and there are 4 ways to arrange the last digit of a number whose digits are multiples of 3.
So total ways = 3*4 = 12 so the answer will be 12.
- From the above illustration, if M is the count of all multiples of X, then the required count of N digit numbers(for N > 1) is equal to (M – 1)*MN – 1.
- For one-digit numbers, the answer is M.
Below is the implementation of the above approach:
C++
#include <stdio.h>
#define ll long long
ll power(ll x, ll n)
{
ll temp;
if (n == 0)
return 1;
temp = power(x, n / 2);
if (n % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
ll count_Total_Numbers(ll n, ll x)
{
ll total, multiples = 0;
for ( int i = 0; i < 10; i++) {
if (i % x == 0)
multiples++;
}
if (n == 1)
return multiples;
total = (multiples - 1)
* power(multiples, n - 1);
return total;
}
int main()
{
ll N = 1, X = 3;
printf ( "%lld " ,
count_Total_Numbers(N, X));
return 0;
}
|
Java
class GFG{
static int power( int x, int n)
{
int temp;
if (n == 0 )
return 1 ;
temp = power(x, n / 2 );
if (n % 2 == 0 )
return temp * temp;
else
return x * temp * temp;
}
static int count_Total_Numbers( int n,
int x)
{
int total, multiples = 0 ;
for ( int i = 0 ; i < 10 ; i++)
{
if (i % x == 0 )
multiples++;
}
if (n == 1 )
return multiples;
total = (multiples - 1 ) *
power(multiples, n - 1 );
return total;
}
public static void main(String[] args)
{
int N = 1 , X = 3 ;
System.out.printf( "%d " ,
count_Total_Numbers(N, X));
}
}
|
Python3
def power(x, n):
temp = []
if (n = = 0 ):
return 1
temp = power(x, n / / 2 )
if (n % 2 = = 0 ):
return temp * temp
else :
return x * temp * temp
def count_Total_Numbers(n, x):
total, multiples = 0 , 0
for i in range ( 10 ):
if (i % x = = 0 ):
multiples + = 1
if (n = = 1 ):
return multiples
total = ((multiples - 1 ) *
power(multiples, n - 1 ))
return total
if __name__ = = '__main__' :
N = 1
X = 3
print (count_Total_Numbers(N, X))
|
C#
using System;
class GFG{
static int power( int x, int n)
{
int temp;
if (n == 0)
return 1;
temp = power(x, n / 2);
if (n % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
static int count_Total_Numbers( int n,
int x)
{
int total, multiples = 0;
for ( int i = 0; i < 10; i++)
{
if (i % x == 0)
multiples++;
}
if (n == 1)
return multiples;
total = (multiples - 1) *
power(multiples, n - 1);
return total;
}
public static void Main()
{
int N = 1, X = 3;
Console.Write(count_Total_Numbers(N, X));
}
}
|
Javascript
<script>
function power(x, n)
{
let temp;
if (n == 0)
return 1;
temp = power(x, parseInt(n / 2));
if (n % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
function count_Total_Numbers(n, x)
{
let total, multiples = 0;
for (let i = 0; i < 10; i++)
{
if (i % x == 0)
multiples++;
}
if (n == 1)
return multiples;
total = (multiples - 1)
* power(multiples, n - 1);
return total;
}
let N = 1, X = 3;
document.write(
count_Total_Numbers(N, X));
</script>
|
Time Complexity: O(Log N)
Auxiliary Space: O(1)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...