Euclid's algorithm, extended Euclid, Chinese remainder theorem

Euclidean algorithm (Euclidean algorithm)

Euclidean algorithm is also called the tossing and dividing method, which is mainly used to calculate the greatest common divisor of two integers a and b.

Simply talk about the principle of the algorithm: the greatest common divisor of two integers is equal to the greatest common divisor of the smaller one and the larger divided by the small remainder.
That is: gcd(a,b)=gcd(b,a mod b).

Give a simple example: For
example, find the greatest common divisor a = gcd(10, 24) of 10 and 24:

  1. Finding the greatest common divisor of 10 and 24 is equal to finding the greatest common divisor of 10 and 4:
    a = gcd(10, 24) = gcd(10, 4)
  2. Finding the greatest common divisor of 10 and 4 is equal to finding the greatest common divisor of 4 and 2, which is 2:
    a = gcd(10, 24) = gcd(10, 4) = gcd(4, 2) = 2
# python
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)
print(gcd(10,24)) # 2

Extended Euclidean algorithm

Algorithm principle: If a and b are positive integers, there are integers x, y such that gcd(a,b)=ax+by; in
layman's terms, gcd(a,b) can be expressed as a linear combination of integers of a and b.

Give a simple example:
gcd(10, 24) = 2
2 = 10*(-7) + 24*3

The main applications are as follows:

  1. Solve the indeterminate equation;
    example: find a set of integer solutions of 435x + 783y = 87:

    First get through Euclid's algorithm:

     783 = 1× 435 + 348
     435 = 348×1 + 87
     348 = 87 × 4 + 0
     ∴ 87 = 435 – 348
     87 = 435 – (783 – 435)
     87 = (–1)(783) + 2(435)
     ∴ x = 2, y = −1是此不定方程的一组整数解。
    
  2. Solve the inverse element of the modulus (multiplicative inverse element), refer to the previous article on congruence equations, Euler functions, multiplicative inverse elements, and matrix inversion defined on Zm ;

  3. Solve modular linear equations (linear congruence equations);

    1. Solve the congruence equation ax ≡ b (mod m), x =?
      Take an extremely representative example: 15x = 1 mod 26 This
      problem is transformed into 15x-26y = 1 can be used as 1 to solve the indefinite equation, or as 2 Find the multiplicative inverse

       解法如下:
       26 = 1× 15 + 11
       15 = 11×1 + 4
       11 = 4 × 2 + 3
       4 = 1 × 3 + 1
       3 = 1 × 3 
       
       ∴ 1 = 4 – 3
       	  = 4 – (11 – 4×2)
       	  = 4×3 – 11
       	  = (15-11) ×3 - 11
       	  = 15×3 - 11×4
       	  = (26-11)×3 - 11 ×4
       	  = 26×3 - (26 - 15)×7
       	  =26×(-4) + 15×7
       ∴ x = 7, y = −4 为此不定方程的一组整数解,15关于模26的乘法逆元为7
      
    2. Solve the system of congruence equations and continue to look at the Chinese remainder theorem

Chinese remainder theorem

There is a question in "Sun Tzu Suan Jing": "Today there are things that do not know the number, two of the three or three are left (divide by 3 and remain 2), and
three of the five and five are left (divide by 5 and remain 3), seven or seven. Two of the remaining numbers (divide by 7 remain 2), what is the matter?"

Song Dynasty mathematician Qin Jiushao made a complete and systematic answer to the problem of "things do not know the number" in Volume 1 and 2 of "Nine Chapters of Mathematics" in 1247. Ming Dynasty mathematician Cheng Dawei compiled the solution into the easy-to-spond "Sun Tzu Ge Jue":

三人同行七十稀,
五树梅花廿一支,
七子团圆正半月,
除百零五便得知。

It means that as long as there is a 1 remaining after dividing by 3, a 70 is added;
as long as a 1 is remaining after dividing by 5, a 21 is added;
as long as a 1 is remaining after dividing by 7, a 15 is added. Then add up.
Finally, calculate the remainder of this sum divided by 105.
That is (2×70 + 3×21 + 15×2) mod 105 = 23

The solution is as follows:

First find out the smaller numbers 15, 21, 70 that are divided by 7, 5, and 3 from the common multiples of 3 and 5, 3 and 7, 5 and 7, respectively (this step is also called "modulo inverse" "Operation, refer to the multiplication inverse element solution). That is:
15÷7=2……Remaining 1,
21÷5=4……Remaining 1,
70÷3=23……Remaining 1.
Then use the three smaller numbers found to multiply the required number by 7. The product of the remainder obtained by dividing, 5, and 3 is continuously added,
15×2+21×3+70×2=233.
Finally, divide 233 by the least common multiple of the three divisors of 3, 5, and 7.
233÷105=2 ...... The remainder is 23,
this remainder 23 is the smallest number that meets the conditions.

Extend to the general situation:
assuming that the integers m1, m2,…, mn are mutually prime, then for any integer: a1, a2,…, an equation system:
Insert picture description here
there are integer solutions, and if X, Y satisfy the equation system , There must be X ≡ Y (mod N) where: The
Insert picture description here
formula is as follows:
Insert picture description here

I really don’t want to look at the formula symbols in the textbook. Let’s take the homework and give two examples.

Assignment 1:

Solve the system of congruence equations:
x ≡ 12 (mod 25)
x ≡ 9 (mod 26)
x ≡ 23 (mod 27)

The above equations are equivalent to x = 25a + 12 = 26b + 9 = 27c + 23.
Shift the terms to get:
①: 25a-27c = 23-12 = 11
②: 26b-25a = 12-9 = 3

Firstly, use Euclidean to expand Euclidean:

27 = 25×1 + 2
25 = 2×7 + 11
则: 
11 = 25 - 2×7
	 = 25 - (27-25) ×7
	 = 25×8 - 27×7
所以a=8, c=7 
代入x = 25a + 12  = 27c + 23 得:
x = 212

Get the combined equation x = 212 + 25 × 27t, that is: x ≡ 212 (mod 675)
and then merge with x ≡ 9 (mod 26)

x = 212 + 675t = 26b + 9
26b - 675t = 203
675 = 26×25+25
25 = 25×1
所以:  
203 = (26-25)×203
	= (26 - (675-26*25))×203
	=  26×5278 - 675×203
b=5278 , t=203
代入得x = 137237

The combined equation x = 137237 + 25 × 27×26t is obtained: x ≡ 137237 (mod 17550), x=14387
x = 14387 + 17550n (n∈Z)

Assignment 2:

Solve the following congruence equations:
13x ≡ 4 (mod 99)
15x ≡ 56 (mod 101)

This kind of congruence equations with coefficients makes people daunting, but it does not prevent the use of extended Euclidean,
first remove the coefficients:
x ≡ 46 (mod 99)
x ≡ 98 (mod 101)

求解方法很多,这里列举利用二元一次不定方程方法:
13x ≡ 4 (mod 99) 转化为 13x-99y = 4
然后用拓展欧几里德:
13×46-99×6 = 4
x=46, y=6
所以不定方程13x-99y = 4 的所有解为
x=46 + 99t
y=6+13t
所以原同余方程解为:x ≡ 46 (mod 99)

Eliminate x to get: 99a-101b = 52
Expand Euclidean to walk you: x = 7471 (mod 9999)
x = 9999 n + 7471 (n ∈ Z)

Guess you like

Origin blog.csdn.net/Pioo_/article/details/111154372