This time to bring you the Python implementation of the method of solving greatest common divisor, Python implementation of the greatest common divisor to solve the points of attention, the following is the actual case, together to see.
The first excerpt from the online description of an algorithm is as follows:
More subtractive method: Also known as more subtractive damage, is derived from the "Nine Chapters arithmetic" a greatest common divisor algorithm, it was originally designed for numerator, but it is suitable for any need to require greatest common divisor occasions.
"Nine Chapters of Arithmetic" is an ancient Chinese mathematics monographs, wherein "more subtractive damage" can be used to seek two number of greatest common divisor, that is, "can half of the half, not half, the sub-denominator, the number of children, in order to reduce less, more subtract loss, and so on." By the number of equal. ”
Translated into modern languages as follows:
The first step: arbitrarily given two positive integers, judging whether they are even. If so, use a 2 reduction, or the second step if not.
The second step: reduce the smaller number by a larger number, and then compare the resulting difference with the smaller number, and subtract the decimal number by the large numbers. Continue this operation until the resulting meiosis and difference are equal.
After reading the above description, my first reaction is that the description is not a problem? In terms of universality, there should be a problem. For example, if I solve the greatest common divisor of 4 and 4, but after half a half, the result must be wrong! The following algorithm is not able to proceed!
In any case, first implement the above algorithm description:
#-*-Coding:utf-8-*-#! Python2def maxcommpisor (m,n): # even process while m% 2 = = 0 and n% 2 = = 0: m = m/2 n = n/2 # EX Change Order when needed if M < n: m,n = n,m # calculate the max Comm Pisor while m-n! = N: diff = m-n if diff > N: m = diff else: m = n n = diff return nprint (Maxcommpisor (55,120)) PRI NT (Maxcommpisor (55,77)) print (Maxcommpisor (32,64)) print (Maxcommpisor (16,128))
Needless to say, the above procedures are riddled with errors. So how do we correct it?
First of all, the other 2 should eventually be counted back! In this way, the program is modified as follows:
def maxcommpisor (m,n): com_factor = 1 if m = = N: return n else: # process for even number while M % 2 = = 0 and n% 2 = = 0: m = Int (M/2) n = Int (N/2) com_factor *= 2 if M < n: m,n = n,m di FF = M-n while n! = diff: m = diff if m < n: m,n = n,m diff = m-n return n * com_fact Orprint (Maxcommpisor (55,120)) print (Maxcommpisor (55,77)) print (Maxcommpisor (32,64)) print (Maxcommpisor (16,128))
By modification, the above program execution results are as follows
Although this program is a bit strange to look at, but the overall algorithm has been implemented. This has a certain probability of decreasing at the level of the loop compared with the algorithm of the division of the other. In particular, the final two sets of test numbers, in this case, the effect is better. But, overall the efficiency of the algorithm, now I can not give an accurate measurement.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Summary of Pycharm's use tips
How Python obtains two-dimensional array local peaks