Given an integer **N**, the task is to find the smallest **N** digit number divisible by all possible prime digits, i.e, **2, 3, 5 **and **7**. Print **-1** if no such number is possible.**Examples:**

Input:N = 5Output:10080Explanation:10080 is the smallest five-digit number that is divisible by 2, 3, 5 and 7.

Input:N = 3Output:210

**Approach: **

Follow the steps given below to solve the problem:

- Since all the four numbers
**2, 3, 5, 7**are prime it means**N**will also be divisible by their product**2 × 3 × 5 × 7 = 210** - For
**N < 3**, no such number exists. So, print**-1**. - For
**N = 3**, the answer will be**210**. - For
**N > 3**, the following computation needs to be done:- Find Remainder
**R = 10**.^{N-1}% N - Add
**210 – R**to**10**.^{N-1}

- Find Remainder

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum number of ` `// n digits divisible by all prime digits ` `void` `minNum(` `int` `n) ` `{ ` ` ` `if` `(n < 3) ` ` ` `cout << -1; ` ` ` `else` ` ` `cout << (210 * ((` `int` `)(` `pow` `(10, n - 1) / ` ` ` `210) + 1)); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `minNum(n); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG{ ` ` ` `// Function to find the minimum number of ` `// n digits divisible by all prime digits ` `static` `void` `minNum(` `int` `n) ` `{ ` ` ` `if` `(n < ` `3` `) ` ` ` `System.out.println(-` `1` `); ` ` ` `else` ` ` `System.out.println(` `210` `* ( ` ` ` `(` `int` `)(Math.pow(` `10` `, n - ` `1` `) / ` `210` `) + ` `1` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `minNum(n); ` `} ` `} ` ` ` `// This code is contributed by Stuti Pathak ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` `from` `math ` `import` `*` ` ` `# Function to find the minimum number of ` `# n digits divisible by all prime digits. ` `def` `minNum(n): ` ` ` `if` `n < ` `3` `: ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `print` `(` `210` `*` `(` `10` `*` `*` `(n` `-` `1` `) ` `/` `/` `210` `+` `1` `)) ` ` ` `# Driver Code ` `n ` `=` `5` `minNum(n) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum number of ` `// n digits divisible by all prime digits ` `static` `void` `minNum(` `int` `n) ` `{ ` ` ` `if` `(n < 3) ` ` ` `Console.WriteLine(-1); ` ` ` `else` ` ` `Console.WriteLine(210 * ` ` ` `((` `int` `)(Math.Pow(10, n - 1) / 210) + 1)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 5; ` ` ` `minNum(n); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

10080

**Time complexity: **O(logN) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if the sum of digits of number is divisible by all of its digits
- Possible to make a divisible by 3 number using all digits in an array
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Smallest n digit number divisible by given three numbers
- Smallest N digit number divisible by N
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find smallest possible Number from a given large Number with same count of digits
- Count of N-digit numbers having digit XOR as single digit
- Check if the first and last digit of the smallest number forms a prime
- Queries for the smallest and the largest prime number of given digit
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest N digit number with none of its digits as its divisor
- Count all prime numbers in a given range whose sum of digits is also prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.