Given an integer N, the task is to find the smallest number greater than or equal to N such that it is divisible by all of its non-zero digits.
Examples:
Input: N = 31
Output: 33
Explanation: 33 is the smallest number satisfying the given condition.
At Unit’s place: 33%3 = 0
At One’s place: 33%3 = 0
Input: N = 30
Output: 30
Explanation: 30 is the smallest number satisfying the given condition.
At One’s place: 30%3 = 0
Approach: Smallest number which is divisible by all digits from 1 to 9 is equal to the LCM of (1, 2, 3, 4, 5, 6, 7, 8, 9) = 2520. Therefore, the multiples of 2520 are also divisible by all digits from 1 to 9 implying that (N + 2520) will always satisfy the condition. Therefore, iterate in the range [N, 2520 + N] and check for the smallest number satisfying the given condition. Follow the steps below to solve the problem:
- Initialize ans as 0 to store the smallest number greater than or equal to N such that it is divisible by all its non-zero digits.
- Iterate over the range [N, N + 2520] using the variable i.
- Initialize a variable possible as 1 to check if the current number i satisfies the given condition or not.
- Get all non-zero digits of i and check if i is divisible by each of them. If found to be true, then update possible to 1, and update ans as i, and break out of the loop.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSmallestNumber( int n)
{
for ( int i = n; i <= (n + 2520); ++i) {
bool possible = 1;
int temp = i;
while (temp) {
if (temp % 10 != 0) {
int digit = temp % 10;
if (i % digit != 0) {
possible = 0;
break ;
}
}
temp /= 10;
}
if (possible == 1) {
cout << i;
return ;
}
}
}
int main()
{
int N = 31;
findSmallestNumber(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findSmallestNumber( int n)
{
for ( int i = n; i <= (n + 2520 ); ++i)
{
int possible = 1 ;
int temp = i;
while (temp != 0 )
{
if (temp % 10 != 0 )
{
int digit = temp % 10 ;
if (i % digit != 0 )
{
possible = 0 ;
break ;
}
}
temp /= 10 ;
}
if (possible == 1 )
{
System.out.println(i);
return ;
}
}
}
public static void main(String[] args)
{
int N = 31 ;
findSmallestNumber(N);
}
}
|
Python3
def findSmallestNumber(n):
for i in range (n, n + 2521 ):
possible = 1
temp = i
while (temp):
if (temp % 10 ! = 0 ):
digit = temp % 10
if (i % digit ! = 0 ):
possible = 0
break
temp / / = 10
if (possible = = 1 ):
print (i, end = "")
return
if __name__ = = "__main__" :
N = 31
findSmallestNumber(N)
|
C#
using System;
class GFG{
static void findSmallestNumber( int n)
{
for ( int i = n; i <= (n + 2520); ++i)
{
int possible = 1;
int temp = i;
while (temp != 0)
{
if (temp % 10 != 0)
{
int digit = temp % 10;
if (i % digit != 0)
{
possible = 0;
break ;
}
}
temp /= 10;
}
if (possible == 1)
{
Console.Write(i);
return ;
}
}
}
public static void Main(String[] args)
{
int N = 31;
findSmallestNumber(N);
}
}
|
Javascript
<script>
function findSmallestNumber(n)
{
for (i = n; i <= (n + 2520); ++i)
{
var possible = 1;
var temp = i;
while (temp != 0)
{
if (temp % 10 != 0)
{
var digit = temp % 10;
if (i % digit != 0)
{
possible = 0;
break ;
}
}
temp = parseInt(temp / 10);
}
if (possible == 1)
{
document.write(i);
return ;
}
}
}
var N = 31;
findSmallestNumber(N);
</script>
|
Time Complexity: O(2520*log10N)
Auxiliary Space: O(1)