I used maths from 2 wiki articles, will blur it in case somebody consider it a spoiler.

Research is part of the challenge I guess

The thing is: my solution is cool for cases, when **n** is ‘appropriate’ function parameter, meaning:

3 numbers: *a, b, c* is Pythagorean triple.

a + b + c = **n**

For cases when **n** is not fit in the above conditions, my code will loop infinitely

I want to implement some terminal condition for outer loop(aka while loop), but I’m so stuck after all this maths, and I need some ideas. Any hint is greatly appreciated.

```
function specialPythagoreanTriplet(n) {
//all Pyth. primitive triples can be generated from
//'root' triple a = 3 b = 4 c = 5
//non-primitive triples can be generated by simple multiplication
//of primitive ones
//step 1
//every subarray >>> Pyth. primitive triple
let storageOfPrimitiveTriples = [[3, 4, 5]];
while (true) {
for (let primitiveTriple of storageOfPrimitiveTriples) {
const sumOfabc = primitiveTriple.reduce(
(a, b) => a + b, 0
);
//if we found primitive triple such as
//sumOfabc is divisor of n:
//can get result
//for non-primitive triple
//with simple math
if (n % sumOfabc === 0) {
primitiveTriple = primitiveTriple.map((num) => (num * n / sumOfabc));
return primitiveTriple.reduce(
(a, b) => a * b, 1
);
}
}
//if did not found result >>> need to generate next
//level of primitive triples, and get rid of current level
let newLevel = [];
for (let primitiveTriple of storageOfPrimitiveTriples) {
newLevel.push(
[
primitiveTriple[0] - primitiveTriple[1] * 2 + primitiveTriple[2] * 2,
primitiveTriple[0] * 2 - primitiveTriple[1] + primitiveTriple[2] * 2,
primitiveTriple[0] * 2 - primitiveTriple[1] * 2 + primitiveTriple[2] * 3
]
);
newLevel.push(
[
primitiveTriple[0] + primitiveTriple[1] * 2 + primitiveTriple[2] * 2,
primitiveTriple[0] * 2 + primitiveTriple[1] + primitiveTriple[2] * 2,
primitiveTriple[0] * 2 + primitiveTriple[1] * 2 + primitiveTriple[2] * 3
]
)
newLevel.push(
[
- primitiveTriple[0] + primitiveTriple[1] * 2 + primitiveTriple[2] * 2,
- primitiveTriple[0] * 2 + primitiveTriple[1] + primitiveTriple[2] * 2,
- primitiveTriple[0] * 2 + primitiveTriple[1] * 2 + primitiveTriple[2] * 3
]
)
}
storageOfPrimitiveTriples = newLevel;
}
}
console.log(specialPythagoreanTriplet(1000));
```