Project Euler 91
Project Euler 91
题目
Right triangles with integer coordinates
The points $P (x_1, y_1)$ and $Q (x_2, y_2)$ are plotted at integer co-ordinates and are joined to the origin, $O(0,0)$, to form $\triangle OPQ$.

There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between $0$ and $2$ inclusive; that is, $0 \leq x_1, y_1, x_2, y_2 \leq 2$.

Given that $0 \leq x_1, y_1, x_2, y_2 \leq 50$, how many right triangles can be formed?
解决方案
由于只有$51\times 51-1$个点,因此每次可以枚举出两个点,通过判断向量内积是否为$0$来找出直角。
将枚举出来的三角形的每一条边都视为一个向量,两两做内积,就可以完成判断,找到一个直角三角形。
通过查询OEIS该数列的前几项,发现结果为A155154。在PROG一栏中,发现如下信息:
1 | (PARI) a(n)=3*n^2+sum(a=1, n, sum(b = 1, n, 2*min(b*gcd(a, b)\a, (n - a)*gcd(a, b)\b) ) ) \\ Yurii Ivanov, Jun 25 2021 |
通过这个信息,可以以$O(n^2\log n)$计算出本题的结果:
代码
1 | N = 50 |
1 | from tools import gcd |