This article mainly introduces the Python implementation of the greatest common divisor algorithm, involving Python mathematical operations related operations skills, the need for friends can refer to the next
In this paper, the solution greatest common divisor algorithm for Python implementation is described. Share to everyone for your reference, as follows:
Using Python to solve the two number of greatest common divisor is used in the previously described decomposition of the mass-type. In fact, I write the decomposition of the quality of the process is because of the discovery in the implementation of the greatest common divisor solution to use this function.
The comparison makes me happy is that before learning a bit of Python collection processing function Incredibly at this time also put in handy, small program completion let people feel more comfortable.
The code is implemented as follows:
#!/usr/bin/pythonfrom Collections Import counterdef primenum (num): R_value =[] for i InRange (2,num+1): For Jin Ra Nge (2,i): If I% J = 0:break Else:r_value.append (i) return r_valuedef primefactorsolv E (num,prime_list): for n inprime_list:if num% n = = 0:return [n,num/n]def primepisor (num): Num_temp = Num prime_range= primenum (num) ret_value =[] While numnot in prime_range:factor_list= primefactorsolve (num,pri Me_range) Ret_value.append (factor_list[0]) num =factor_list[1] else:ret_value.append (num) return Count ER (ret_value) def maxpisor (num1,num2): Dict1 =primepisor (NUM1) dict2 =primepisor (num2) max_pisor= 1 for Key1 indict 1:if key1 in Dict2:if Dict1[key1] < Dict2[key1]: max_pisor*= (Key1 * dict1[key1]) el se:max_pisor*= (key1 * * Dict2[key1]) return Max_pisorprint (Maxpisor (12,18)) print (Maxpisor (7,2)) print (Maxpiso R (7,13)) print (Maxpisor (24,56)) Print (Maxpisor (63,81))
The results of the execution of the program are as follows:
E:\WorkSpace\01_ programming language \03_python\math>python max_pisor.py
6
1
1
8
9
Through verification, the calculation results are accurate.