【Mathematics】Pei Shu Theorem

The Peishu theorem, also known as the Beizu theorem, is a theorem discovered and proved by the ancient Chinese mathematician Pei Shu (about the 3rd century AD) in the "Sun Tzu Mathematical Classics".

Pei Shu's theorem refers to the greatest common divisor d of two integers a and b, which can be written as the smallest positive integer in any integer ax + by (a, b, x, and y are all integers) in the following form, where x and y are unknown integer. This theorem is widely used in cryptography, digital signal processing and other fields.

The method of proving Pei Shu's theorem can be realized by the method of proof by contradiction. Suppose ax+by=c has a set of positive integer solutions c, where c is not a multiple of the greatest common divisor d of a and b. Therefore, there is another positive integer d'=gcd(c,d), and d' cannot be divided by d. Define e=c/d' and express c as d'e, then:

d' = ax' + by'
d  = ax'' + by''

Then, we can deduce:

e = cx''/dd' + cy''/dd' = (ae+b)/d'x'' + (be+a)/d'y''

According to this equation, it can be seen that both ae and be are multiples of d', and their sum is also a multiple of d'. However, since d' and d have no common factors, ae and be only have multiples of d (the same goes for ax and by). Thus, we arrive at a contradiction: e should be a multiple of d, but since it can be expressed as a sum of different single parts, it must be a common multiple of d and d'. This is inconsistent with our initial assumption, so ax+by=d has the smallest positive integer solution among all positive integers x and y, which is Pei Shu's theorem.

In addition, the extended Euclidean algorithm provides a method to solve Pei Shu's theorem, which is to find any coefficient x and y in the form of ax + by = d through the reverse Euclidean algorithm.

1. 如果b=0,则令d=a,x=1,y=0,并返回(d,x,y)
2. 否则,递归调用函数来计算gcd(b, a mod b),并设得到的结果为(d', x', y')。
3. 根据贝祖等式可以得到以下结果:
    d = d'
    x = y'
    y = x' - (a / b) * y'
4. 返回(d, x, y)

It should be noted that in theory, this algorithm can only solve the case where a and b are mutually prime, but in practice, we can also avoid problems by ensuring the sum of the absolute values ​​of x and y is the smallest during the calculation process.

Guess you like

Origin blog.csdn.net/Runcode8/article/details/130789521