Пример #1
0
def leastCommonMultiple(number1,number2):
	
	try:
		# Numbers need to be integerized because of sys.argv sending strings
		number1 = int(number1)
		number2 = int(number2)
	except:
		# Return error message if either input is not a number
		print("Invalid input. Please use only integer values as arguments.")
		return 0

	# Get prime factors of both numbers
	pf1 = primeFactors(number1)
	pf2 = primeFactors(number2)

	# Collect all prime factors of either number
	# all_factors is a list that will later be multiplied together
	all_factors = []
	# For each element 'x' in pf1
	for x in pf1:
		# Append element to all_factors
		all_factors.append(x)
		# Remove element from pf2 to avoid unnecessary duplicates
		if x in pf2:
			pf2.remove(x)
	
	# Add in the unused factors from pf2
	all_factors += pf2
	
	# Define least common multiple variable as lcm, equal to 1
	lcm = 1

	# Get product of all prime factors
	for y in all_factors:
		lcm *= y

	# Return least common multiple
	return lcm