Greatest common divisor for solving two integers (not negative numbers) (requires two numbers not being 0)
When both numbers are 0 o'clock, greatest common divisor is 0.
Way one: the poor lifting method
1 defGCU (M, n):2 if notm:3 returnN4 elif notN:5 returnm6 elifM isN:7 returnm8 9 ifM >N:TenGCD =N One Else: AGCD =m - - whileM%gcdorn%GCD: theGCD-= 1 - - returngcd
View Code
Method Two: Phase subtraction
1 defGCU (M, n):2 if notm:3 returnN4 elif notN:5 returnm6 elifM isN:7 returnm8 9 whilem!=N:Ten ifM>N: OneM-=N A Else: -N-=m - the returnM
View Code
Way three: Euclid's Euclidean method
1 defGCU (M, n):2 if notm:3 returnN4 elif notN:5 returnm6 elifM isN:7 returnm8 9 whilem%N:Tenm, n = N, m%N One A returnN
View Code
Mode four: Euclid's method of dividing the number of miles to achieve recursion
1 defGCU (m,n):2 if notN:3 returnm4 Else:5 returnGCU (N, m%N)6 7 if __name__=='__main__':8a = Int (input ('Please input the first integers:'))9b = Int (input ('Please input the second integers:'))Tenresult =GCU (A, b) One Print('GCU =', result)
View Code
Python Greatest common divisor