I'm trying to solve the Project Euler Problem 9 :
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
I looked on Wikipedia for the formula to find Pythagorean triples and tried to translate it into code. The problem is that the code is outputting the wrong answer, but I think that the code is correct.
var a, b, c;
var pos1, pos2, pos3;
var ans1, ans2, ans3;
for(var n=2; n<=20000; n++) {
a = 2 * n + 1;
b = 2 * n * (n +1);
c = 2 * n * (n +1) + 1;
if(a<b<c) {
if(a^2 + b^2 === c^2) {
pos1 = a;
pos2 = b;
pos3 = c;
}
if(a + b + c ===1000) {
ans1 = a;
ans2 = b;
ans3 = c;
}
}
}
console.log(ans1 + " " + ans2 + " " + ans3);
for (int a=0;a < 1000;a++) {for (int b=0;b<1000;b++) { for (int c=0;c<1000;c++) { if a + b + c == 1000) { return a + "," + b + "," + c;}}}}
a^2 + b^2 === c^2
toMath.pow(a,2) + Math.pow(b,2) === Math.pow(c,2)
invalidates the answers. If you "fix" the code in the question it makes the question less relevant because nobody can see the problem. Please correct the code by posting the corrected code as an answer, or by placing the corrected code after a note saying "Edit:" See:Editing questions to fix incorrect code