Smallest N digit number with none of its digits as its divisor
Last Updated :
22 Nov, 2021
Given an integer N. The task is to find the smallest N digit number S, such that S is not divisible by any of its digits. Print -1 if no such number is possible.
Examples:
Input: N = 2
Output: 23
Explanation: 23 is the smallest two-digit number that is not divisible by any of its digits.
Input: N = 1
Output: -1
Explanation: Every single digit number is divisible by itself.
Input: N = 4
Output: 2227
Explanation: 2227 is the smallest four-digit number that is not divisible by any of its digits.
Approach:
- The approach is to find the smallest and largest possible N-digit number say these numbers are L and R respectively.
- Iterate over the range [L, R].
- For each number in the range, extract every digit of that number and check if the number is divisible by that digit.
- If at least one digit of the number divides it, discard that number and iterate to check the next number.
- If none of the digits of that number divides it, then print that number and break out of the loop. This number so found is the smallest number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int a, int b)
{
if (b == 0)
return 1;
if (b == 1)
return a;
int tmp = power(a, b / 2);
int result = tmp * tmp;
if (b % 2 == 1)
result *= a;
return result;
}
bool check( int n)
{
int temp = n;
while (temp > 0) {
int last_digit = temp % 10;
if (last_digit == 0
|| last_digit == 1)
return false ;
if (n % last_digit == 0)
return false ;
temp = temp / 10;
}
return true ;
}
void solve( int n)
{
int L = power(10, n - 1);
int R = power(10, n) - 1;
int flag = 0;
for ( int i = L; i <= R; i++) {
bool answer = check(i);
if (answer == true ) {
cout << i << endl;
flag++;
break ;
}
}
if (flag == 0)
cout << -1 << endl;
}
int main()
{
int N = 4;
solve(N);
}
|
Java
import java.util.*;
class GFG{
static int power( int a, int b)
{
if (b == 0 )
return 1 ;
if (b == 1 )
return a;
int tmp = power(a, b / 2 );
int result = tmp * tmp;
if (b % 2 == 1 )
result *= a;
return result;
}
static boolean check( int n)
{
int temp = n;
while (temp > 0 )
{
int last_digit = temp % 10 ;
if (last_digit == 0 ||
last_digit == 1 )
return false ;
if (n % last_digit == 0 )
return false ;
temp = temp / 10 ;
}
return true ;
}
static void solve( int n)
{
int L = power( 10 , n - 1 );
int R = power( 10 , n) - 1 ;
int flag = 0 ;
for ( int i = L; i <= R; i++)
{
boolean answer = check(i);
if (answer == true )
{
System.out.println(i);
flag++;
break ;
}
}
if (flag == 0 )
System.out.println( "-1" );
}
public static void main(String[] args)
{
int N = 4 ;
solve(N);
}
}
|
Python3
def power(a, b):
if (b = = 0 ):
return 1 ;
if (b = = 1 ):
return a;
tmp = power(a, b / / 2 );
result = tmp * tmp;
if (b % 2 = = 1 ):
result * = a;
return result;
def check(n):
temp = n;
while (temp > 0 ):
last_digit = temp % 10 ;
if (last_digit = = 0 or
last_digit = = 1 ):
return False ;
if (n % last_digit = = 0 ):
return False ;
temp = temp / / 10 ;
return True ;
def solve(n):
L = power( 10 , n - 1 );
R = power( 10 , n) - 1 ;
flag = 0 ;
for i in range (L, R + 1 ):
answer = check(i);
if (answer):
print (i)
flag + = 1 ;
break ;
if (flag = = 0 ):
print ( - 1 )
if __name__ = = "__main__" :
N = 4 ;
solve(N);
|
C#
using System;
class GFG{
static int power( int a, int b)
{
if (b == 0)
return 1;
if (b == 1)
return a;
int tmp = power(a, b / 2);
int result = tmp * tmp;
if (b % 2 == 1)
result *= a;
return result;
}
static bool check( int n)
{
int temp = n;
while (temp > 0)
{
int last_digit = temp % 10;
if (last_digit == 0 ||
last_digit == 1)
return false ;
if (n % last_digit == 0)
return false ;
temp = temp / 10;
}
return true ;
}
static void solve( int n)
{
int L = power(10, n - 1);
int R = power(10, n) - 1;
int flag = 0;
for ( int i = L; i <= R; i++)
{
bool answer = check(i);
if (answer == true )
{
Console.WriteLine(i);
flag++;
break ;
}
}
if (flag == 0)
Console.WriteLine( "-1" );
}
public static void Main(String[] args)
{
int N = 4;
solve(N);
}
}
|
Javascript
<script>
function power(a, b)
{
if (b == 0)
return 1;
if (b == 1)
return a;
let tmp = power(a, Math.floor(b / 2));
let result = tmp * tmp;
if (b % 2 == 1)
result *= a;
return result;
}
function check(n)
{
let temp = n;
while (temp > 0) {
let last_digit = temp % 10;
if (last_digit == 0
|| last_digit == 1)
return false ;
if (n % last_digit == 0)
return false ;
temp = Math.floor(temp / 10);
}
return true ;
}
function solve(n)
{
let L = power(10, n - 1);
let R = power(10, n) - 1;
let flag = 0;
for (let i = L; i <= R; i++) {
let answer = check(i);
if (answer === true ) {
document.write(i + "<br>" );
flag++;
break ;
}
}
if (flag === 0)
document.write( "-1" + "<br>" );
}
let N = 4;
solve(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...