Record Python implementation of greatest common divisor & minimum number of public digits two algorithms
Concept
Greatest common divisor: refers to the largest of two or more integers in total
Least common multiple: two or more integers of the public are called their common multiple, where the smallest common multiple other than 0 is called the least common multiple of these integers.
The relationship between the two: the product of two numbers = least common multiple * greatest common divisor
Instance
Euclidean method
A=int (Input (' Please enter 1st num: ') "B=int (Input (' Please enter 2nd num: ') s=a*bwhile a%b!=0: a,b=b, (a%b) Else : print (b, ' was the maximum common divisor ') print (s//b, ' is the least common multiple ') #运行结果please enter 1st Num:10 Please enter 2nd num:155 are the Maximum common Divisor30 is the Least common multiple
The method of more subtractive loss
A=int (Input (' Please enter 1st num: ') b=int (input (' "Please enter 2nd num: ')") s=a*b while a!=b: if a>b: a-= b elif a<b: b-=aelse: Print ( A, ' was the maximum common divisor ') print (s//a, ' is the least common Multiple ') #运行结果please enter 1st Num:40please Enter 2nd num:6020 is the maximum common divisor120 is the least common multi Ple
The difference between the division method and the more subtractive loss (1) is the method to find the maximal common factor, the calculation of the divided method to divide-based, the more subtractive loss of the main subtraction, the calculation of the number of divided method is relatively small number of calculations, especially when the number of two different size difference is more obvious. (2) from the manifestation of the results, the result of the method is that the dividing remainder is 0, and the subtraction is obtained by the same difference between the meiosis and the differential.
Python implementation for greatest common divisor and least common multiple