寻找毕达哥拉斯三胞胎的代码

时间:2011-10-09 15:50:45

标签: java

我目前正在尝试这个问题:

  毕达哥拉斯三重奏是一组三个自然数a,b和c,为此   a 2 + b 2 = c 2

     

例如,3 2 + 4 2 = 9 + 16 = 25 = 5 2

     

恰好存在一个毕达哥拉斯三重态,其中a + b + c = 1000。   找到产品abc。

我的代码如下,我认为应该是正确的,但网站告诉我我的答案是错的?有人可以帮助我看看我的逻辑中的缺陷吗?

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

6 个答案:

答案 0 :(得分:11)

以下是 5个解决方案(从慢到快):

1)琐碎的实施 - 732857微秒(0.7秒)

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)限制b&amp;的下限c(建立顺序关系) - 251091微秒(0.2秒)

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)限制下限&amp; b&amp; b的上限c - 111220微秒(0.1秒)

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)限制下限&amp; b的上限和c的固定值 - 2625微秒

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)使用 Euclid 的公式 - 213微秒

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

答案 1 :(得分:7)

我认为你错过了一套大括号。缩进使我相信两个最内层的陈述是一致的,但你需要花括号来证明这一点。

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

如果没有大括号product将始终包含abc最后值的乘积。 (333 * 500 * 167 == 27805500)。

答案 2 :(得分:2)

你可以这样试试,

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

这实现了欧几里德生成毕达哥拉斯三元组的公式here

请注意,在这种方法中,我们只制作三元组,因此减少了不必要的重复。

,输出为a:375 b:200 c:425 Product为31875000

答案 3 :(得分:2)

虽然其他人已经为您的代码提供了特定的修补程序,但这里有一个更为通用的提示,对其他问题也很有用。 在更简单的问题版本上测试您的代码。

例如,查看你的程序是否可以找到6,8,10作为三元组,总和为24.使用较小的测试,你可以实际执行代码以查看它出错的地方。

答案 4 :(得分:2)

/ /

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

答案 5 :(得分:0)

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;
    }
  }
}
相关问题