Files
freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/project-euler/problem-9-special-pythagorean-triplet.md
2022-01-20 20:30:18 +01:00

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5900f3761000cf542c50fe88 問題 9: 特殊なピタゴラスの三つ組数 5 302205 problem-9-special-pythagorean-triplet

--description--

ピタゴラスの三つ組数とは、次の式を満たし、a < b < c である 3 つの自然数の集合です。

a2 + b2 = c2

例えば、32 + 42 = 9 + 16 = 25 = 52 です。

a + b + c = 1000 となるピタゴラスの三つ組数が 1 つだけ存在します。 a + b + c = n となるような積 abc を求めなさい。

--hints--

specialPythagoreanTriplet(24) は数値を返す必要があります。

assert(typeof specialPythagoreanTriplet(24) === 'number');

specialPythagoreanTriplet(24) は 480 を返す必要があります。

assert.strictEqual(specialPythagoreanTriplet(24), 480);

specialPythagoreanTriplet(120) は 49920, 55080, 60000 のいずれかを返す必要があります。

assert([49920, 55080, 60000].includes(specialPythagoreanTriplet(120)));

specialPythagoreanTriplet(1000) は 31875000 を返す必要があります。

assert.strictEqual(specialPythagoreanTriplet(1000), 31875000);

--seed--

--seed-contents--

function specialPythagoreanTriplet(n) {
 let sumOfabc = n;

 return true;
}

specialPythagoreanTriplet(1000);

--solutions--

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;
 }
 }
 }
}