This article mainly introduces Python's method of solving greatest common divisor based on the Euclidean method, and analyzes the implementation method and optimization operation skill of Python using the greatest common divisor method, and the need of friends can refer to the following
In this paper, we describe the method of Python to solve greatest common divisor based on the method of Euclidean division. Share to everyone for your reference, as follows:
Before summarizing the greatest common divisor solution in Gartner TAOCP, in fact, the algorithm modification in the after-class problem is to solve the greatest common divisor by the method of the Euclidean division.
My initial understanding of this topic was wrong, and naturally I did not make the standard answer. Now follow the answer to the answer to write the corresponding code implementation:
#-*-Coding:utf-8-*-#! Python2def maxcommpisor (m,n): While m * n! = 0: m = m% n if m = = 0: return n else: n = n% m if n = = 0: return mprint (Maxcommpisor (55,120))
Execution results of the program:
Swap the position of two digits with the following code:
#-*-Coding:utf-8-*-#! Python2def maxcommpisor (m,n): While m * n! = 0: m = m% n if m = = 0: return n else: n = n% m< c14/>if n = = 0: return mprint (Maxcommpisor (120,55))
Execution results of the program:
It is mentioned in the topic that the efficiency can be reduced by the above code, and the loss of efficiency should be based on division and judgment. Here, compare the code of the previous algorithm:
def commdevisor (m,n): r = m% n while r! = 0: m = n n = r r = m% n return Nprint (Commdevisor (120, 25))
Operation Result:
The new algorithm has a more division and comparison operation in the loop. In fact, the efficiency of comparison is good, but the operation of Division will lead to the reduction of efficiency.
PS: Here again for you to recommend several calculation tools for further reference:
Online unary function (equation) Solver calculation tool:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi
Scientific Calculator Online Use _ Advanced calculator online calculation:
Http://tools.jb51.net/jisuanqi/jsqkexue
Online Calculator _ Standard calculator:
Http://tools.jb51.net/jisuanqi/jsq