Find a N-digit number such that it is not divisible by any of its digits
Given an integer N, the task is to find any N-digit positive number (except for zeros) such that it is not divisible by any of its digits. If it is not possible to find any such number then print -1.
Note: There can be more than one such number for the same N-digit.
Examples:
Input: N = 2
Output: 23
23 is not divisible by 2 or 3
Input: N = 3
Output: 239
Approach:
The easiest solution to this problem can be thought of with the help of digits ‘4’ and ‘5’.
- Since, in order for a number to be divisible by 5, the number must end with 0 or 5; and in order for it to be divisible by 4, the last two digits if the number must be divisible by 4.
- Therefore, a shortcut method can be applied to prevent both of the divisibility criteria of 4 and as well as of 5, as:
- To prevent a number from being divisible by 5, the number can contain 5 for every other digit except for last digit.
Therefore for N digit number,
(N - 1) digits must be 5 = 5555...(N-1 times)d
where d is the Nth digit
- To prevent a number from being divisible by 4, the number can contain 5 at the second last digit and 4 at the last digit.
Therefore for N digit number,
Last digit must be 4 = 5555...(N-1 times)4
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void findTheNumber( int n)
{
if (n == 1) {
cout << "Impossible" << endl;
return ;
}
for ( int i = 0; i < n - 1; i++) {
cout << "5" ;
}
cout << "4" ;
}
int main()
{
int n = 12;
findTheNumber(n);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static void findTheNumber( int n)
{
if (n == 1 ) {
System.out.print( "Impossible" + "\n" );
return ;
}
for ( int i = 0 ; i < n - 1 ; i++) {
System.out.print( "5" );
}
System.out.print( "4" );
}
public static void main(String[] args)
{
int n = 12 ;
findTheNumber(n);
}
}
|
Python3
def findTheNumber(n):
if (n = = 1 ):
print ( "Impossible" )
return
for i in range (n - 1 ):
print ( "5" ,end = "")
print ( "4" )
if __name__ = = '__main__' :
n = 12
findTheNumber(n)
|
C#
using System;
class GFG{
static void findTheNumber( int n)
{
if (n == 1) {
Console.Write( "Impossible" + "\n" );
return ;
}
for ( int i = 0; i < n - 1; i++) {
Console.Write( "5" );
}
Console.Write( "4" );
}
public static void Main(String[] args)
{
int n = 12;
findTheNumber(n);
}
}
|
Javascript
<script>
function findTheNumber(n)
{
if (n == 1) {
document.write( "Impossible" );
return ;
}
for ( var i = 0; i < n - 1; i++) {
document.write( "5" );
}
document.write( "4" );
}
var n = 12;
findTheNumber(n);
</script>
|
Time complexity: O(N), where N is the required size of the number.
Auxiliary Space: O(1), as constant space is required.
Optimized Approach:
Here are some optimizations you can make to the code:
- Remove unnecessary header file: You don’t need to include the entire “bits/stdc++.h” header file. You can replace it with the specific header files that you need, which in this case are <iostream> and <string>.
- Use a string instead of cout: Instead of printing the number digit by digit, you can create a string variable to store the number and then print the whole string at once.
- Use a single loop: You can combine the two loops in the original code into a single loop that generates the number digit by digit.
Here’s the optimized code:
C++
#include <iostream>
#include <string>
using namespace std;
void findTheNumber( int n)
{
if (n == 1) {
cout << "Impossible" << endl;
return ;
}
string number(n-1, '5' );
number += '4' ;
cout << number << endl;
}
int main()
{
int n = 12;
findTheNumber(n);
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static void findTheNumber( int n) {
if (n == 1 ) {
System.out.println( "Impossible" );
return ;
}
StringBuilder number = new StringBuilder();
for ( int i = 0 ; i < n - 1 ; i++) {
number.append( "5" );
}
number.append( "4" );
System.out.println(number.toString());
}
public static void main(String[] args) {
int n = 12 ;
findTheNumber(n);
}
}
|
Python3
def findTheNumber(n):
if n = = 1 :
print ( "Impossible" )
return
number = "5" * (n - 1 ) + "4"
print (number)
n = 12
findTheNumber(n)
|
C#
using System;
public class MainClass {
public static void FindTheNumber( int n) {
if (n == 1) {
Console.WriteLine( "Impossible" );
return ;
}
string number = "" ;
for ( int i = 0; i < n - 1; i++) {
number += "5" ;
}
number += "4" ;
Console.WriteLine(number);
}
public static void Main( string [] args) {
int n = 12;
FindTheNumber(n);
}
}
|
Javascript
function findTheNumber(n) {
if (n == 1) {
console.log( "Impossible" );
return ;
}
let number = "5" .repeat(n - 1) + "4" ;
console.log(number);
}
let n = 12;
findTheNumber(n);
|
Time complexity: O(N), where N is the required size of the number.
Auxiliary Space: O(1), as constant space is required.
Last Updated :
16 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...