This is my code so far.
from math import gcd
#3 digit lcm calculation
h=input("(1) 2 Digit LCM Or \n(2) 3 Digit LCM\n :")
if h == "2":
while True:
def lcm(x, y, z):
a = gcd(x, y, z)
num = x
num2 = y * z // a
LCM = num * num2 // a
return LCM
x = int(input("Number 1: "))
y = int(input("Number 2: "))
z = int(input("Number 3: "))
print("The LCM Of " + str(x) + " And " + str(y) + " And " + str(z) + " Is " + str(lcm(x, y, z)))
if h == "1":
while True:
def lcm(x, y):
a = gcd(x, y)
num = x
num2 = y
LCM = num * num2 // a
return LCM
x = int(input("Number 1: "))
y = int(input("Number 2: "))
print("The LCM Of " + str(x) + " And " + str(y) + " Is " + str(lcm(x, y)))
My problem is that the 3 digit just finds a common multiple not the lowest such a 10 , 5 , 8 makes 400 instead of a possible 40. Any help would be useful!
New Code Thanks To Prune
from math import gcd
#3 digit lcm calculation
h=input("(1) 2 Digit LCM Or \n(2) 3 Digit LCM\n :")
if h == "2":
while True:
def lcm(x, y, z):
gcd2 = gcd(y, z)
gcd3 = gcd(x, gcd2)
lcm2 = y*z // gcd2
lcm3 = x*lcm2 // gcd(x, lcm2)
return lcm3
x = int(input("Number 1: "))
y = int(input("Number 2: "))
z = int(input("Number 3: "))
print("The LCM Of " + str(x) + " And " + str(y) + " And " + str(z) + " Is " + str(lcm(x, y, z)))
One other thing, Is there another way to mark code instead of having to add 4 spaces in before each and every line. Thanks
ANALYSIS
As you have just discovered (but not yet realized), the relation that holds for pairs of integers:
x * y = GCD(x, y) * LCM(x, y)
does not hold for triples. The basic logic for prime factorization is that the GCD takes the minimum exponent for each prime factor; the LCD takes the maximum exponent. With only two integers, this means that each exponent gets used exactly once, allowing the above equation to hold.
However, with three integers, you guarantee that the middle exponent for each prime factor will be excluded from both the LCM and GCD computations.
10 = 2^1 * 3^0 * 5^1
8 = 2^3 * 3^0 * 5^0
5 = 2^0 * 3^0 * 5^1
----------------------
GCD = 2^0 * 3^0 * 5^0
LCM = 2^3 * 3^0 * 5^1
Note the excluded factors: 2^1 * 3^0 * 5^1 = 10, which is why your LCM computation is high by a factor of 10.
SOLUTION
You need to split the logic when you employ the third integer, something like this:
# Find the 2-number and 3-number GCDs
gcd2 = gcd(y, z)
gcd3 = gcd(x, gcd2)
# Find the 2-number and 3-number LCMs
lcm2 = y*z // gcd2
lcm3 = x*lcm2 // gcd(x, lcm2)
return lcm3