I write a program that find Pythagorean triple of a number. the code is below.
public static void pythagoreanTriplet(int n) {
int i, j, k, count = 0;
for (i = 1; i <= n; i ) {
if (n == i)
break;
j = (n * n - 2 * n * i) / (2 * n - 2 * i);
k = n - i - j;
if ((i * i j * j == k * k) && j > 0 && k > 0) {
System.out.println(i " " j " " k);
count ;
break;
}
}
if (count != 1) {
System.out.println("Impossible");
}
}
This code works correctly for all numbers except one. if input n = 408, the output should be 119 120 169, but my code output is 102 136 170.
CodePudding user response:
There could be more than one solution for this pythagorean triplet,condition, the "break" in your second "if" terminates after it reaches lower number i.e 102, it never makes it to 119,