Why do I have negative 'a' here?

I am trying to come up with some kind of fancy solution for this:

Right now I am building some function which will allow to generate Pythagorean triples. Wikipedia for this stuff

Can’t figure out why am I getting some negative numbers with the below code.

for (let n = 1; n<= 10; n++) {
  const m = n++;
  const a = Math.pow(m, 2) - Math.pow(n, 2);
  const b = 2 * m * n;
  const c = Math.pow(m, 2) + Math.pow(n, 2);
  console.log('for m:', m, 'for n:', n);
  console.log('a: ', a, 'b: ', b, 'c: ', c);
  console.log((Math.pow(a, 2) + Math.pow(b, 2)), Math.pow(c, 2));
  console.log('---------------');
}

/*
Output:

for m: 1 for n: 2
a:  -3 b:  4 c:  5
25 25
---------------
for m: 3 for n: 4
a:  -7 b:  24 c:  25
625 625
---------------
for m: 5 for n: 6
a:  -11 b:  60 c:  61
3721 3721
---------------
for m: 7 for n: 8
a:  -15 b:  112 c:  113
12769 12769
---------------
for m: 9 for n: 10
a:  -19 b:  180 c:  181
32761 32761
---------------
*/

Look at your console output. If m is 1 and n is 2 then what do you get with this line:

const a = Math.pow(m, 2) - Math.pow(n, 2);

Thanks!!
Oh, I see. I think this:

was an outstanding example of nonsense from me :joy:

1 Like

Don’t beat yourself up too much. These problems are intended to be hard. Sometimes there is a neat trick for solving them but it usually includes some pretty advanced math. I’m no mathematician so I don’t claim to know how to solve this one, that’s for sure.

1 Like

No, I am not going to blame myself. This one was kinda fun actually)
Usually this kind of mistakes means that little break is needed)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.