Largest N digit number divisible by given three numbers
Last Updated :
21 Mar, 2023
Given four integers x, y, z, and n, the task is to find the largest n digit number which is divisible by x, y, and z.
Examples:
Input: x = 2, y = 3, z = 5, n = 4
Output: 9990
9990 is the largest 4-digit number which is divisible by 2, 3 and 5.
Input: x = 3, y = 23, z = 6, n = 2
Output: Not possible
Approach:
- Find the largest n digit number i.e. pow(10, n) - 1 and store it in a variable largestN.
- Find LCM of the given three numbers x, y and z say LCM.
- Calculate the remainder when largestN is divided by LCM i.e. largestN % LCM and store it in a variable remainder.
- Subtract remainder from largestN. If the result is still an n digit number then print the result.
- Else print Not possible.
Below is the implementation of the above approach:
C++
// C++ program to find largest n digit number
// which is divisible by x, y and z.
#include <bits/stdc++.h>
using namespace std;
// Function to return the LCM of three numbers
int LCM(int x, int y, int z)
{
int ans = ((x * y) / (__gcd(x, y)));
return ((z * ans) / (__gcd(ans, z)));
}
// Function to return the largest n-digit
// number which is divisible by x, y and z
int findDivisible(int n, int x, int y, int z)
{
// find the LCM
int lcm = LCM(x, y, z);
// find largest n-digit number
int largestNDigitNum = pow(10, n) - 1;
int remainder = largestNDigitNum % lcm;
// If largest number is the answer
if (remainder == 0)
return largestNDigitNum ;
// find closest smaller number
// divisible by LCM
largestNDigitNum -= remainder;
// if result is an n-digit number
if (largestNDigitNum >= pow(10, n - 1))
return largestNDigitNum;
else
return 0;
}
// Driver code
int main()
{
int n = 2, x = 3, y = 4, z = 6;
int res = findDivisible(n, x, y, z);
// if the number is found
if (res != 0)
cout << res;
else
cout << "Not possible";
return 0;
}
Java
// Java program to find largest n digit number
// which is divisible by x, y and z.
import java.math.*;
class GFG {
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
// Function to return the LCM of three numbers
static int LCM(int x, int y, int z)
{
int ans = ((x * y) / (gcd(x, y)));
return ((z * ans) / (gcd(ans, z)));
}
// Function to return the largest n-digit
// number which is divisible by x, y and z
static int findDivisible(int n, int x, int y, int z)
{
// find the LCM
int lcm = LCM(x, y, z);
// find largest n-digit number
int largestNDigitNum = (int)Math.pow(10, n) - 1;
int remainder = largestNDigitNum % lcm;
// If largest number is the answer
if (remainder == 0)
return largestNDigitNum ;
// find closest smaller number
// divisible by LCM
largestNDigitNum -= remainder;
// if result is an n-digit number
if (largestNDigitNum >= (int)Math.pow(10, n - 1))
return largestNDigitNum;
else
return 0;
}
// Driver code
public static void main(String args[])
{
int n = 2, x = 3, y = 4, z = 6;
int res = findDivisible(n, x, y, z);
// if the number is found
if (res != 0)
System.out.println(res);
else
System.out.println("Not possible");
}
}
Python3
# Python3 program to find largest n digit
# number which is divisible by x, y and z.
# Recursive function to return
# gcd of a and b
def gcd(a, b):
# Everything divides 0
if (a == 0):
return b;
if (b == 0):
return a;
# base case
if (a == b):
return a;
# a is greater
if (a > b):
return gcd(a - b, b);
return gcd(a, b - a);
# Function to return the LCM
# of three numbers
def LCM(x, y, z):
ans = ((x * y) / (gcd(x, y)));
return ((z * ans) / (gcd(ans, z)));
# Function to return the largest n-digit
# number which is divisible by x, y and z
def findDivisible(n, x, y, z):
# find the LCM
lcm = LCM(x, y, z);
# find largest n-digit number
largestNDigitNum = int(pow(10, n)) - 1;
remainder = largestNDigitNum % lcm;
# If largest number is the answer
if (remainder == 0):
return largestNDigitNum ;
# find closest smaller number
# divisible by LCM
largestNDigitNum -= remainder;
# if result is an n-digit number
if (largestNDigitNum >= int(pow(10, n - 1))):
return largestNDigitNum;
else:
return 0;
# Driver code
n = 2; x = 3;
y = 4; z = 6;
res = int(findDivisible(n, x, y, z));
# if the number is found
if (res != 0):
print(res);
else:
print("Not possible");
# This code is contributed
# by mits
C#
// C# program to find largest n
// digit number which is divisible
// by x, y and z.
using System;
class GFG
{
// Recursive function to return
// gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
// Function to return the
// LCM of three numbers
static int LCM(int x, int y, int z)
{
int ans = ((x * y) / (gcd(x, y)));
return ((z * ans) / (gcd(ans, z)));
}
// Function to return the largest
// n-digit number which is divisible
// by x, y and z
static int findDivisible(int n, int x,
int y, int z)
{
// find the LCM
int lcm = LCM(x, y, z);
// find largest n-digit number
int largestNDigitNum = (int)Math.Pow(10, n) - 1;
int remainder = largestNDigitNum % lcm;
// If largest number is the answer
if (remainder == 0)
return largestNDigitNum ;
// find closest smaller number
// divisible by LCM
largestNDigitNum -= remainder;
// if result is an n-digit number
if (largestNDigitNum >= (int)Math.Pow(10, n - 1))
return largestNDigitNum;
else
return 0;
}
// Driver code
static void Main()
{
int n = 2, x = 3, y = 4, z = 6;
int res = findDivisible(n, x, y, z);
// if the number is found
if (res != 0)
Console.WriteLine(res);
else
Console.WriteLine("Not possible");
}
}
// This code is contributed by ANKITRAI1
PHP
<?php
// PHP program to find largest n digit number
// which is divisible by x, y and z.
// Recursive function to return gcd of a and b
function gcd($a, $b)
{
// Everything divides 0
if ($a == 0)
return $b;
if ($b == 0)
return $a;
// base case
if ($a == $b)
return $a;
// a is greater
if ($a > $b)
return gcd($a - $b, $b);
return gcd($a, $b - $a);
}
// Function to return the LCM
// of three numbers
function LCM($x, $y, $z)
{
$ans = (($x * $y) / (gcd($x, $y)));
return (($z * $ans) / (gcd($ans, $z)));
}
// Function to return the largest n-digit
// number which is divisible by x, y and z
function findDivisible($n, $x, $y, $z)
{
// find the LCM
$lcm = LCM($x, $y, $z);
// find largest n-digit number
$largestNDigitNum = (int)pow(10, $n) - 1;
$remainder = $largestNDigitNum % $lcm;
// If largest number is the answer
if ($remainder == 0)
return $largestNDigitNum ;
// find closest smaller number
// divisible by LCM
$largestNDigitNum -= $remainder;
// if result is an n-digit number
if ($largestNDigitNum >= (int)pow(10, $n - 1))
return $largestNDigitNum;
else
return 0;
}
// Driver code
$n = 2; $x = 3; $y = 4; $z = 6;
$res = findDivisible($n, $x, $y, $z);
// if the number is found
if ($res != 0)
echo $res;
else
echo "Not possible";
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// Javascript program to find largest n
// digit number which is divisible
// by x, y and z.
// Recursive function to return
// gcd of a and b
function gcd(a, b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
// Function to return the
// LCM of three numbers
function LCM(x, y, z)
{
var ans = parseInt((x * y) / (gcd(x, y)));
return parseInt((z * ans) / (gcd(ans, z)));
}
// Function to return the largest
// n-digit number which is divisible
// by x, y and z
function findDivisible(n, x, y, z)
{
// find the LCM
var lcm = LCM(x, y, z);
// find largest n-digit number
var largestNDigitNum = Math.pow(10, n) - 1;
var remainder = largestNDigitNum % lcm;
// If largest number is the answer
if (remainder == 0)
return largestNDigitNum ;
// find closest smaller number
// divisible by LCM
largestNDigitNum -= remainder;
// if result is an n-digit number
if (largestNDigitNum >= Math.pow(10, n - 1))
return largestNDigitNum;
else
return 0;
}
// Driver code
var n = 2, x = 3, y = 4, z = 6;
var res = findDivisible(n, x, y, z);
// if the number is found
if (res != 0)
document.write(res);
else
document.write("Not possible");
</script>
Time Complexity: O(log(min(x, y,z ))) + O(log(n)) as we are doing lcm of x,y,z we need log(min(x,y,z)) time complexity for that + log(n) for doing pow(10,n-1) so overall time complexity will be O(log(min(x, y,z ))) + O(log(n))
Auxiliary Space: O(log(min(x, y, z))) + O(log(n)) as we are doing lcm of x,y,z this lcm will be done in recursively manner so recursion need extra O(log(min(x, y, z))) auxiliary stack space, and addition for doing pow(10,n-1) which is also in recursive manner which also need log(n) extra auxiliary stack space
Similar Reads
Smallest n digit number divisible by given three numbers
Given x, y, z and n, find smallest n digit number which is divisible by x, y and z. Examples: Input : x = 2, y = 3, z = 5 n = 4Output : 1020Input : x = 3, y = 5, z = 7 n = 2Output : Not possibleRecommended PracticeMighty DivisorTry It!Method: Brute-forceThe brute-force approach to solve this problem
15+ min read
Largest K digit number divisible by all numbers in given array
Given an array arr[] of size N and an integer K. The task is to find the largest K digit number divisible by all number of arr[]. Examples: Input: arr[] = {2, 3, 5}, K = 3Output: 990Explanation: 990 is the largest 3 digit number divisible by 2, 3 and 5. Input: arr[] = {91, 93, 95}, K = 3Output: -1Ex
6 min read
Sum of n digit numbers divisible by a given number
Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.Examples: Input : n = 2, number = 7Output : 728Explanation: There are thirteen n digit numbers that are divisible by 7. Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
9 min read
Count n digit numbers divisible by given number
Given number of digit n and a number, the task is to count all the numbers which are divisible by that number and having n digit. Examples : Input : n = 2, number = 7Output : 13Explanation: There are nine n digit numbers that are divisible by 7. Numbers are 14, 21, 28, 35, 42, 49, .... 91, 98 Input
8 min read
Kth largest N digit number divisible by M
Given three positive integers N, K, and M. The task is to find Kth largest N digit number divisible by M. Note: K will be such an integer that Kth largest N digit number divisible by M always exists. Examples Input: N = 2, K = 2, M = 2Output: 96Explanation: The 2nd largest 2 digit number divisible b
5 min read
Divide number into two parts divisible by given numbers
Given a large number in string format and we are also given two numbers f and s. We need to divide the large number into two continuous parts such that the first part is divisible by f and the second part is divisible by s. Examples: Input: num = â246904096â f = 12345 s = 1024 Output: Yes We can div
11 min read
Smallest N digit number divisible by N
Given a positive integers N, the task is to find the smallest N digit number divisible by N. Examples: Input: N = 2 Output: 10 Explanation: 10 is the smallest 2-digit number which is divisible by 2. Input: N = 3 Output: 102 Explanation: 102 is the smallest 3-digit number which is divisible by 3. Nai
6 min read
Find the Largest number with given number of digits and sum of digits
Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v
13 min read
Smallest K digit number divisible by all numbers in given array
Given an array arr[]. The task is to create the smallest K digit number divisible by all numbers of arr[]. Examples: Input: arr[] = {2, 3, 5}, N = 3Output: 120Explanation: 120 is divisible by 2, 3 and 5 Input: arr[] = {2, 6, 7, 4, 5}, N = 5Output: 10080 Recursive approach: This problem can be solved
7 min read
Smallest number divisible by first n numbers
Given a number n find the smallest number evenly divisible by each number 1 to n.Examples: Input : n = 4 Output : 12 Explanation : 12 is the smallest numbers divisible by all numbers from 1 to 4 Input : n = 10 Output : 2520 Input : n = 20 Output : 232792560If you observe carefully the ans must be th
8 min read