2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3931000cf542c50fea6
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 问题39:整数直角三角形
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: problem-39-integer-right-triangles
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
如果p是具有整数长度边的直角三角形的周长{a,b,c},则对于p = 120,恰好有三个解。{20,48,52},{24,45,51},{ 30,40,50}对于p≤n的值,最大化解的数量是多少?
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`intRightTriangles(500)`应该返回420。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(intRightTriangles(500) == 420);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`intRightTriangles(800)`应该返回420。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(intRightTriangles(800) == 720);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`intRightTriangles(900)`应该返回840。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(intRightTriangles(900) == 840);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`intRightTriangles(1000)`应该返回840。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(intRightTriangles(1000) == 840);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function intRightTriangles(n) {
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intRightTriangles(500);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
// Original idea for this solution came from
|
|
|
|
|
// https://www.xarg.org/puzzle/project-euler/problem-39/
|
|
|
|
|
|
|
|
|
|
function intRightTriangles(n) {
|
|
|
|
|
// store the number of triangles with a given perimeter
|
|
|
|
|
let triangles = {};
|
|
|
|
|
// a is the shortest side
|
|
|
|
|
for (let a = 3; a < n / 3; a++)
|
|
|
|
|
// o is the opposite side and is at least as long as a
|
|
|
|
|
for (let o = a; o < n / 2; o++) {
|
|
|
|
|
let h = Math.sqrt(a * a + o * o); // hypotenuse
|
|
|
|
|
let p = a + o + h; // perimeter
|
|
|
|
|
if ((h % 1) === 0 && p <= n) {
|
|
|
|
|
triangles[p] = (triangles[p] || 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let max = 0, maxp = null;
|
|
|
|
|
for (let p in triangles) {
|
|
|
|
|
if (max < triangles[p]) {
|
|
|
|
|
max = triangles[p];
|
|
|
|
|
maxp = parseInt(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return maxp;
|
|
|
|
|
}
|
|
|
|
|
```
|