Open In App

Smallest N digit number divisible by all possible prime digits

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 = 5 
Output: 10080 
Explanation: 10080 is the smallest five-digit number that is divisible by 2, 3, 5 and 7.
Input: N = 3 
Output: 210



Approach: 
Follow the steps given below to solve the problem: 
 

Below is the implementation of the above approach:
 






// 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




// 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




# 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)




// 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




<script>
// Javascript implementation of the above approach 
 
// Function to find the minimum number of 
// n digits divisible by all prime digits
function minNum(n)
{
    if (n < 3)
        document.write( -1);
    else
        document.write((210 * (parseInt(Math.pow(10, n - 1) /
                                   210) + 1)));
}
 
// Driver Code
var n = 5;
minNum(n);
 
// This code is contributed by rrrtnx.
</script>

Output: 
10080

 

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


Article Tags :