2.0 KiB
2.0 KiB
id, challengeType, title, forumTopicId, localeTitle
id | challengeType | title | forumTopicId | localeTitle |
---|---|---|---|---|
5900f3761000cf542c50fe88 | 5 | Problem 9: Special Pythagorean triplet | 302205 | Задача 9: Специальный пифагорейский триплет |
Description
a 2 + b 2 = c 2
Например, 3 2 + 4 2 = 9 + 16 = 25 = 5 2 . Существует ровно один пифагорейский триплет, для которого a + b + c = 1000. Найдите произведение abc такое, что a + b + c = n
.
Instructions
Tests
tests:
- text: <code>specialPythagoreanTriplet(1000)</code> should return 31875000.
testString: assert.strictEqual(specialPythagoreanTriplet(1000), 31875000);
- text: <code>specialPythagoreanTriplet(24)</code> should return 480.
testString: assert.strictEqual(specialPythagoreanTriplet(24), 480);
- text: <code>specialPythagoreanTriplet(120)</code> should return 49920, 55080 or 60000
testString: assert([49920, 55080, 60000].includes(specialPythagoreanTriplet(120)));
Challenge Seed
function specialPythagoreanTriplet(n) {
let sumOfabc = n;
// Good luck!
return true;
}
specialPythagoreanTriplet(1000);
Solution
const specialPythagoreanTriplet = (n)=>{
let sumOfabc = n;
let a,b,c;
for(a = 1; a<=sumOfabc/3; a++){
for(b = a+1; b<=sumOfabc/2; b++){
c = Math.sqrt(a*a+b*b);
if((a+b+c) == sumOfabc){
return a*b*c;
}
}
}
}