Code for searching Pythagorean triplets

I am currently asking this question:

The Pythagorean triplet is a set of three natural numbers, a, b and c, for which a 2 + b 2 = c 2 .

For example, 3 2 + 4 2 = 9 + 16 = 25 = 5 2 .

There is exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

My code is as follows, I think it should be correct, but the site tells me that my answer is incorrect? Can someone help me see the flaws in my logic, please?

public class Pythagoras { public static void main(String[] args) { int sum = 1000; int a; int product=0; for (a = 1; a <= sum/3; a++) { int b; for (b = a + 1; b <= sum/2; b++) { int c = sum - a - b; if ( c > 0 && (a*a + b*b == c*c) ) System.out.printf("a=%d, b=%d, c=%d\n",a,b,c); product = a * b * c; } } System.out.println(product); } } 
+7
source share
6 answers

I think you are missing a set of braces. The indentation leads me to believe that the two innermost statements go together, but you need braces to make it right.

 if ( c > 0 && (a*a + b*b == c*c) ) { System.out.printf("a=%d, b=%d, c=%d\n",a,b,c); product = a * b * c; } 

Without curly braces, product will always contain the product of the values last a , b and c . (333 * 500 * 167 == 27805500).

+7
source

Here are 5 solutions (from slow to fast):

1) Trivial implementation - 732857 microseconds (0.7 seconds)

 private static void p1(int sum) { for (int a = 0; a <= sum; a++) { for (int b = 0; b <= sum; b++) { for (int c = 0; c <= sum; c++) { if (a < b && b < c && a + b + c == sum && (c * c == a * a + b * b)) { System.out.print(a * b * c); return; } } } } } 

2) Limit the lower limit for b and c (set the order relation) to 251091 microseconds (0.2 seconds)

 private static void p2(int sum) { for (int a = 0; a <= sum; a++) { for (int b = a + 1; b <= sum; b++) { for (int c = b + 1; c <= sum; c++) { if (a + b + c == sum && (c * c == a * a + b * b)) { System.out.print(a * b * c); return; } } } } } 

3) Limit the lower and upper bounds for b and c - 111220 microseconds (0.1 seconds)

 private static void p3(int sum) { for (int a = 0; a <= sum; a++) { for (int b = a + 1; b <= sum - a; b++) { for (int c = b + 1; c <= sum - a - b; c++) { if (a + b + c == sum && (c * c == a * a + b * b)) { System.out.print(a * b * c); return; } } } } } 

4) Limit the lower and upper bounds for b and fix the value for c - 2625 microseconds

 private static void p4(int sum) { for (int a = 0; a <= sum; a++) { for (int b = a + 1; b <= sum - a; b++) { int c = sum - a - b; if (c > b && c * c == a * a + b * b) { System.out.print(a * b * c); return; } } } } 

5) Use the Euclidean formula - 213 microseconds

 private static void p5(int sum) { // a = m^2 - n^2 // b = 2mn // c = m^2 + n^2 int a, b, c; int sqrt = (int)Math.sqrt(sum); for (int n = 1; n <= sqrt; n++) { for (int m = n+1; m <= sqrt; m++) { a = m*m - n*n; b = 2*m*n; c = m*m + n*n; if ( a + b + c == 1000 ) { System.out.print(a * b * c); return; } } } } 
+10
source

You can try this,

 public class Pythagoras { public static void main(String[] args) { int m = 1, n = 0, a = 0, b = 0, c = 0, sum = 0; int product = 0; for (m = 2; m < 100; m++) { for (n = 1; n < 100; n++) { while (m > n) { a = (m * m) - (n * n); b = (2 * m) * n; c = (m * m) + (n * n); sum = a + b + c; if (sum == 1000) { product = a * b * c; System.out.print("a :" + a + "b :" + b + "c : " + c); System.out.println("Product is" + product); break; } break; } } } } } 

This implements the Euclidean formula for generating the Pythagorean triplet, as described here

Please note that in this method we only do triplets, therefore unwanted repetitions are reduced.

and output a : 375 b : 200 c : 425 Product - 31875000

+2
source

While others have already given specific corrections for your code, here is a more general hint that will be useful for other problems. Check your code for a simpler version of the problem.

For example, see if your program can find 6,8,10 as a triple with the sum of 24. With a smaller test, you can really go through the code to see where this happens.

+2
source

//

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public javax.swingx.event.*; public class Triplet extends JApplet implements ActionListener { JLabel l1, l2, l3; JButton b1; JTextFiel t1, t2; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); l1=new JLabel("Enter the value of a: "); l2=new JLabel("Enter the value of b: "); t1 = new JTextField(20); t2 = new JTextField(20); b1=new JButton("Ok"); l2=new JLabel(" "); add(l1); add(t1); add(l2); add(t2); add(b1); add(l3); b1.addActionListener(this); public void ActionPerformed(ActionEvent e) { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); long c = Math.sqrt(a*a + b*b); l3.setText(" " +c); } } } 
+2
source
 public class Pythagorean_Triplets { public static void main(long n) { long h=1,p=1,b1; double b; while(h<=n) { while(p<h) { b=Math.sqrt((h*h)-(p*p)); if(b%1==0) { b1=(long)b; System.out.println(b1+","+p+","+h); break; } p++; } h++; p=1; } } } 
0
source

All Articles