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.
Input: x = 2, y = 3, z = 5, n = 4
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
- 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:
# 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):
if (b == 0):
# base case
if (a == b):
# 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))):
# 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):
# This code is contributed
# by mits
- Largest K digit number divisible by X
- C++ Program for Largest K digit number divisible by X
- Java Program for Largest K digit number divisible by X
- Sum of n digit numbers divisible by a given number
- Count n digit numbers divisible by given number
- Smallest n digit number divisible by given three numbers
- Largest number less than N with digit sum greater than the digit sum of N
- Largest Even and Odd N-digit numbers
- N digit numbers divisible by 5 formed from the M digits
- Largest palindrome which is product of two n-digit numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find nth number that contains the digit k or divisible by k.
- Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Largest even digit number not greater than N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.