开发者

Code for finding pythagorean triplets

I am currently attempting this question :

A Pythagorean triplet is a set of three natural numbers, a, b and 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.

My code is as follows, I think it should be correct, but the site is telling me my answer is wrong? 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.o开发者_运维技巧ut.println(product);
        }
    }


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 bound for b & c (establish the order relation) - 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 & upper bounds for b & 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 lower & upper bounds for b and fix 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 Euclid's 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;
            }
        }
    }
}


I think you're missing a set of braces. The indentation leads me to believe the two innermost statements go together but you need curly braces for that to be correct.

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 the braces product will always contain the product of the last values of a, b, and c. (333 * 500 * 167 == 27805500).


Though others have already given specific fixes for you code, here's a more general hint that will be useful on other problems as well. Test your code on a simpler version of the problem.

For example, see if your program can find 6,8,10 as a triplet with a sum of 24. With a smaller test you can actually step through the code to see where it's going wrong.


You may try it this way,

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 Euclid's formula for generating Pythagorean triplet as explained here

Note that in this method we make only triplets hence unwanted repetitions are reduced.

and the output is a :375 b :200 c : 425 Product is 31875000


//

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);
    }
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜