The greatest common divisor and least common multiple (detail)

When write complete framework "Decreases method", there is a details to note: The results from the function to return, to the result assignment in the main function to a variable in order to follow the code used, or even call that function, but also not the result of the function execution. Because the result returned is a local variable, the function can not cross-use. In that moment to run a custom function in a timely manner will result assigned to a variable
Preface:
And multiple divisors: There exist in the relationship between two integers divisible. That is, if a divisible b, i.e. a% b = 0 (remainder), then a is a multiple of b, b is a number from about a (large is a multiple, small of about several multiples greater than the number equal to about). Such as: 12 ÷ 3 = 4 ... 0, 12 is a multiple of 3, the number of about 12 3
Divisor: Number of public about a few integers
The greatest common divisor : Greatest Common Divisor (GCD) refers to two or more integer numbers of about a maximum. a, b is referred to as the greatest common divisor (a, b), the same, a, b, c referred to as the greatest common divisor (a, b, c), the greatest common divisor of integers have the same sign. Greatest common divisor There are several ways, a common prime factor decomposition method, the short division, Euclidean, Decreases method. And the greatest common divisor corresponding to the least common multiple concepts, a, b referred to as the least common multiple of [a, b]
Examples of 1:12 and 16
There are about 12 ∵ number 1,2,3,4,6,12, and 16 is about several 1,2,4,8,16. (An integer divisors are limited)
∴ divisor: 1,2,4
∴ greatest common divisor: 4
Referred to as (12, 16) = 4, or as (16,12) = 4
The least common multiple : Least Common Multiple (LCM) two or more integer multiples of the public is called a common multiple thereof, wherein other than 0 smallest common multiple of a least common multiple of these is called an integer. Integer a, b referred to as the least common multiple of [a, b], the same, a, b, c is referred to as the least common multiple [a, b, c], the plurality of the least common multiple of an integer of the same sign has
Examples of 2:12 and 16
There 12,24,36,48,60,72,84,96,108 ...... in multiples of 12, and a multiple of 16 have 16,32,48,64,80,96,112 .... .., (an integer multiple of the infinite number
∴ have a common multiple of 48, 96, ......
∴ least common multiple: 48
Referred to as [12, 16] = 48 or [16, 12] = 48
Method for finding the greatest common divisor:
(1) prime factor decomposition method (all are all prime factors)
 
(2) :( Euclidean Euclidean algorithm)
 
Programming ideas: a ÷ b = c ... d (where a> b), when d is 0, then b is the greatest common divisor would otherwise be removed with d b, b case the dividend, divisor d, and in the process, from start to finish, always a symbol representative of the dividend, the divisor b representatives, representative of C supplier, d represents the number of I, b so as to dividend, will become a, i.e. a = b, and d to when the divisor, It becomes necessary to b, i.e., b = d
. 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

Guess you like

Origin www.cnblogs.com/kenny-feng/p/11478925.html