This article mainly introduces the implementation of Python least common multiple algorithm, involving Python numerical operations, judgments and other related operational skills, the need for friends can refer to the following
In this paper, the solution least common multiple algorithm for Python implementation is described. Share to everyone for your reference, as follows:
Simple analysis, the previous introduction of the greatest common divisor solution method is similar to the least common multiple solution method, only need to change a simple condition, and then do a little more simple calculation. The solution of the problem is also based on the decomposition of the quality of the procedure.
The program implementation and the test case code are as follows:
#!/usr/bin/pythonfrom Collections Import counterdef primenum (num): R_value =[] for I in range (2,num+1): for J in Range (2,i): If I% J = 0:break Else:r_value.append (i) return r_valuedef Primefactorso Lve (num,prime_list): for n in prime_list:if num% n = = 0:return [n,num/n]def primepisor (num): Num_tem P =num prime_range= primenum (num) ret_value =[] While Num does in prime_range:factor_list= primefactorsolve (num , Prime_range) Ret_value.append (factor_list[0]) num =factor_list[1] else:ret_value.append (num) return C Ounter (Ret_value) def leastcommonmultiple (num1,num2): Dict1 =primepisor (NUM1) dict2 =primepisor (num2) least_common_mu Ltiple= 1 for key in Dict1:if key in Dict2:if Dict1[key] > Dict2[key]: LEAST_COMMON_MULTIPL e*= (Key * * Dict1[key]) else:least_common_multiple*= (Key * * Dict2[key]) for key in Dict1:if K EY not in Dict2: least_common_multiple*= (Key * * Dict1[key]) for key in Dict2:if key not in dict1:least_common_multiple*= (Key * * Dict2[key]) return Least_common_multipleprint (Leastcommonmultiple (12,18)) print (Leastcommonmultiple (7,2)) Print (Leastcommonmultiple (7,13)) print (Leastcommonmultiple (24,56)) print (Leastcommonmultiple (63,81))
Program execution Results:
E:\WorkSpace\01_ programming Language \03_python\math>pythonleast_common_multiple.py
36
14
91
168
567
Through verification, the calculation results are accurate.