Given four numbers X, Y, Z and N. Find smallest N digit Number that is divisible by all the three numbers X, Y and Z.
Example 1:
Input: 
N = 2
X = 2, Y = 3, Z = 5
Output: 30
Explaination: 30 is the lcm of 2, 3 and 5 
and it is a 2 digit number.
Example 2:
Input: 
N = 2
X = 3, Y = 5, Z = 7
Output: -1
Explaination: There is no 2 digit number 
divisible by 3, 5 and 7.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minNumber() which takes the four number X, Y, Z and N as input parameters and returns the smallest N digit number divisible by X, Y and Z. If ther is no such number, return -1.
Expected Time Complexity: O(Log X)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ X, Y, Z, ≤ 1000
1 ≤ N ≤ 18

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3



class Solution:

    def minNumber(self, X, Y, Z, N):

        # code here

Solutions:
import math

class Solution:

	def lcm(self, x, y, z):
		ans = x * y // math.gcd(x, y)
		return z * ans // math.gcd(ans, z)

	def minNumber(self, X, Y, Z, N):
		maxx = pow(10, N - 1)
		lcm = self.lcm(X, Y, Z)
		rem = maxx % lcm
		if rem == 0:
			return maxx
		if lcm > pow(10, N):
			return -1
		ans = maxx + (lcm - rem)
		return ans


import math

class Solution:

	def minNumber(self, X, Y, Z, N):
		ans1 = X * Y // math.gcd(X, Y)
		ans = ans1 * Z // math.gcd(ans1, Z)
		max1 = 10 ** (N - 1)
		rem = max1 % ans
		if rem == 0:
			return max1
		if ans > pow(10, N):
			return -1
		ans2 = max1 + (ans - rem)
		return ans2


class Solution:

	def minNumber(self, X, Y, Z, N):
		flag = sorted([X, Y, Z])
		maxi = max(flag)
		a = flag[0]
		b = flag[1]
		start = 0
		ans = -1
		for i in range(10 ** (N - 1), 10 ** (N - 1) + maxi):
			if i % maxi == 0:
				start = i
				break
		for i in range(start, 10 ** N - 1, maxi):
			if i % a == 0 and i % b == 0:
				ans = i
				break
		return ans


import math

class Solution:

	def gcd(self, a, b):
		(a, b) = (max(a, b), min(a, b))
		while b:
			(a, b) = (b, a % b)
		return a

	def minNumber(self, X, Y, Z, N):
		lcm = X * Y // self.gcd(X, Y)
		lcm = lcm * Z // self.gcd(lcm, Z)
		if 10 ** N - 1 <= lcm:
			return -1
		return math.ceil(10 ** (N - 1) / lcm) * lcm