有效地计算所有勾股数知道斜边

问题描述:

给定一个斜边( C 中的典型公式 A * A + B * B = C * C )什么是计算 A b ,这样 A< b

Given a hypoteneuse (c in the typical equation a*a + b*b = c*c), what is an efficient way to calculate all possible integer values of a and b, such that a < b?

请注意:我已经看到了 C 大于 1E12 ,从而 C *ç long.MaxValue 更大>,从我可以告诉你, C *ç不适合入小数,虽然

Note: I've seen c be greater than 1e12, thus c*c is greater than long.MaxValue, from what I can tell, c*c does fit into a decimal, though.

有就是找到一个数学解和b快速甚至对C的较大值。
不幸的是,它不是那么简单。我试图给算法的简短说明。我希望这不是太混乱。

There is a mathematical solution that finds a and b fast even for large values of c. Unfortunately, it is not that simple. I'm trying to give a short explanation of the algorithm. I hope it is not too confusing.

由于C,并给出你正在寻找a和b,你基本上要解决的形式不定
方程

Since c is given and you are looking for a and b, you basically want to solve diophantine equations of the form

n=x^2+y^2,

,其中n给出。它没有帮助太多,N = C * c是一个正方形,因此我所描述的对任意n解决方案。如果n是一个素数,那么你可以使用
费马大定理,来决定如果你的方程是可解的,和使用,伦指出,埃尔米特,Serret algoritm找到解决方案,如果有任何。

where n is given. It doesn't help much that n = c*c is a square and thus I'm describing a solution for any n. If n were a prime number then you could use Fermat's theorem, to decide if your equation is solvable, and use, as Moron pointed out the Hermite-Serret algoritm to find the solutions if there are any.

要解决的情况下,其中n是不主要是用
高斯整数的一个好主意。 (高斯整数只是整系数复数)。一个特别的指出,X +彝族的标准的是

To solve the case where n is not prime it is a good idea to use Gaussian integers. (Gaussian integers are just complex numbers with integral coefficients). In particular one notes that the norm of x+yi is

N(x+yi) = x^2+y^2.



因此,人们必须找到高斯整数x +易其规范为n。
由于范数是乘法它足以解决这个方程对于n的因子,然后乘以individal方程的高斯整数。
我举一个例子。我们要解决

Hence one has to find the Gaussian integers x+yi whose norm is n. Since the norm is is multiplicative it is sufficient to solve this equation for the factors of n and then to multiply the Gaussian integers of the individal equations. Let me give an example. We want to solve

65 = x^2 + y^2.

这是等同于求x,y,使得

This is equivalent to find x,y such that

N(x+yi) = 65

在高斯整数。我们因子65 = 5 * 13,那么我们使用费马要注意,两个
5分配和13可被表示为两个平方的总和。最后,我们无论是通过使用埃尔米特,Serret算法

over the Gaussian integers. We factor 65 = 5 * 13, then we use Fermat to note that both 5 and 13 can be represented as sum of two squares. Finally, we find either by using brute force of by using the Hermite-Serret algorithm

N(2+i) = N(1+2i) = ... = 5
N(2+3i) = N(3+2i) = ... = 13

请注意,我已经离开了高斯整数2-1,-2 +我等具有负系数。
那些是当然的解决方案太。

Note, I've left out the Gaussion integers 2-i, -2+i, etc with negative coefficients. Those are of course solutions too.

因此,我们现在可以繁殖这些解决方案一起获得

Hence we can now multiply these solutions together to get

65 = 5 * 13 = N(2 + I)* N(2 + 3I)= N((2 + I)*(2 + 3I))= N(1 + 8I)

65 = 5*13 = N(2+i) * N(2+3i) = N((2+i) * (2+3i)) = N(1 + 8i)

65 = 5 * 13 = N(2 + I)* N(3 + 2I)= N((2 + I) *(3 + 2I))= N(4 + 7I)。

65 = 5 * 13 = N(2+i) * N(3+2i) = N((2+i) * (3+2i)) = N(4 + 7i).

因此,人们得到了两个解决方案

Hence, one gets the two solutions

1*1 + 8*8 = 65
4*4 + 7*7 = 65

其他组合如负系数需要太检查。
他们没有给出比其他排列新的解决方案和改变的迹象。

The other combinations e.g. with negative coefficients need to be checked too. They don't give new solutions other than permutations and changed signs.

要计算所有的解决方案1还可以添加,没有必要计算过C *℃。
查找C的因素是一切必要。还由于和b都是小于c,就不会发生的高斯整数的产品没有与64位的整数系数可表示。因此,如果一个是小心,64位整数是足够的精度,以解决这个问题。当然,它总是容易只使用如Python语言不具有这种溢出问题。

To compute all the solutions one might also add that there is no need to ever compute c*c. Finding the factors of c is all that is necessary. Also since a and b are both smaller than c, it will not happen that products of Gaussian integers are not representable with 64-bit integer coefficients. Hence, if one is careful, 64-bit integer are enough precision to solve the problem. Of course, it is always easier to just use a language like Python that does not have this kind of overflow problems.