题目138:考察高和底边相差1的等腰三角形
Special isosceles trianglesConsider the isosceles triangle with base length, b = 16, and legs, L = 17.
By using the Pythagorean theorem it can be seen that the height of the triangle, h = √(172 − 82) = 15, which is one less than the base length.
With b = 272 and L = 305, we get h = 273, which is one more than the base length, and this is the second smallest isosceles triangle with the property that h = b ± 1.
Find ∑ L for the twelve smallest isosceles triangles for which h = b ± 1 and b, L are positive integers.
题目:
考虑底边 b 长为 16,腰长 L 为 17 的等腰三角形:
利用勾股定理可知此三角形的高 h 为 h = √(172 − 82) = 15,比底边长少 1。
当 b = 272, L = 305 时,可以得到 h = 273,比底边长大 1。而这也是第二小的满足 h = b ± 1 的等腰三角形。
求前 12 个满足此性质的三角形的 L 之和,其中 b 和 L 均为正整数。
程序是很简单,关键是要运行多久的问题。前8个:
16
272
4896
87840
1576240
28284464
59907457
91530450
lst = []
for d in range(2,100000000):
l1 = (0.25*d*d + (d-1)*(d-1))**0.5
l2 = (0.25*d*d + (d+1)*(d+1))**0.5
if l1 == int(l1) or l2 == int(l2):
lst.append(d)
print (d) jerryxjr1220 发表于 2016-9-28 15:38
程序是很简单,关键是要运行多久的问题。前8个:
老哥这个题目的答案是多少啊,我算的是872587755 本帖最后由 guosl 于 2022-9-15 23:34 编辑
先假设b=2y,h=b+1,则L^2=y^2 + (2y+1)^2
L^2 -5b^2 - 4b - 1 = 0;
而这个二次不定方程的整数解有递推式(https://www.alpertron.com.ar/QUAD.HTM)
x(n+1) = - 9x(n) - 20y(n) - 8
y(n+1) = - 4x(n) - 9y(n) - 4
再假设b=2y,h=b-1,则L^2=y^2 + (2y-1)^2
L^2 -5b^2 + 4b - 1 = 0;
而这个二次不定方程的整数解有递推式
x(n+1) = - 9x(n) - 20y(n) + 8;
y(n+1) = - 4x(n) - 9y(n) + 4;
但这两个递推得出的结果就前后相差一项,所以任取一个都行,又对x来讲正负都一样,
而y是负的正好对应于h=b-1的情况。
答案:1118049290473932
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
long long x1 = 1, x2, y1 = 0, y2;
unsigned long long nS = 0;
for (int i = 1; i <= 12; ++i)
{
x2 = -9 * x1 - 20 * y1 - 8;
y2 = -4 * x1 - 9 * y1 - 4;
long long h, b;
b = 2 * abs(y2);
if (y2 < 0)
h = b - 1;
else
h = b + 1;
x1 = x2;
y1 = y2;
nS += abs(x1);
}
cout << nS << endl;
return 0;
}
页:
[1]