. 1 DEF GCD (A, B): # define the greatest common divisor function 2 . 3 IF A < B: . 4 . 5 A, B = B, A . 6 . 7 R & lt. 1 = # as long as the initial value is not 0 can be . 8 . 9 the while = R & lt! 0: 10 . 11 R & lt% = a B 12 is 13 is a = B 14 15 B = R & lt 16 . 17 return a 18 is . 19 20 is 21 is DEF LMG (a, B): # define the least common multiple of the demand function 22 is 23 is return A // B * GCD (A, B) 24 25 26 is 27 DEF main (): 28 29 A = the eval (INPUT ()) 30 31 is B = the eval (INPUT ()) 32 33 is Print ( " Maximum Convention number: " , GCD (A, B)) 34 is 35 Print ( " least common multiple: " , LCM (A, B)) 36 37 [ main ()
The code can also function without the main line (i.e., remove DEF main () and main () these two), after removing not only the implementation of a custom function. But in order to complete rigorous, clear thinking, it is recommended into the main function
(3) Method Decreases:
Notes: Euclidean greatest common divisor efficient
The least common multiple of the Dharma:
Method a: See above (1)
Method two: See above (2), in addition to seeking the greatest common divisor by (a, b) × [a, b] = a × b greatest common divisor (Formula Act)
Notes: Formula to find the least common multiple efficient
. 1 a = the eval (INPUT ()) 2 . 3 B = the eval (INPUT ()) . 4 . 5 IF a < B: . 6 . 7 a, B = B, a . 8 . 9 # ensure a is not less than B 10 . 11 12 is COUNT = 0 13 is 14 IF a% 2 == 0 and b% 2 == 0: # a and b all even, the following code is executed 15 16 the while a% 2 == 0 and b% 2 == 0: # If all even , removing constantly with a 2 and B, until the failure can be an even number (but not necessarily coprime, such as 11 and 77) . 17 18 is a = int (a / 2) #Division returns a float type, need turned int . 19 20 is B = int (B / 2 ) 21 is 22 is COUNT + =. 1 # statistics except 2 how many times 23 is 24 the while ab & = B:! # Important: throughout the program, a is always expressed minuend, b is the subtrahend, ab is poor. And must ensure that no less than a B, the following statement would if this feature is implemented 25 26 is a, B = B, ab & # minuend is calculated for each cycle and a difference between the minuend and subtrahend into assignments, so that the next calculation 27 28 IF a < B: 29 30 a, B = B, a 31 is 32 Print (B * 2 ** count) # multiplied by the power of the finally obtained count subtrahend and 2 33 is 34 is 35 the else : # A and b whether all even, perform the following at 36 37 [ the while ab & =! B: 38 is 39 A, b = b, ab & 40 41 is IF A < b: 42 is 43 is A, b = b, A 44 is 45 Print (b)
Full frame (function package):
1 def abc(a,b): 2 if a<b: 3 a,b = b,a 4 while a-b != b: 5 a,b = b,a-b 6 if a<b: 7 a,b = b,a 8 return b 9 10 def main(): 11 a = eval(input()) 12 b = eval(input()) 13 count = 0 14 if a%2 == 0 and b%2 ==0: 15 the while A% 2 == 0 and B% 2 == 0: 16 A = int (A / 2 ) . 17 B = int (B / 2 ) 18 is COUNT + =. 1 . 19 B = ABC (A, B) 20 is Print ( " The greatest common divisor: " , B * 2 ** COUNT) 21 is the else : 22 is B = ABC (A, B) 23 is Print ( " the greatest common divisor: " , B) 24 main ()
Important: In the main function, the result of the function ABC () to be assigned to a variable, or even return b, b the subsequent code does not take too, but with the original b