project euler 9

来源:互联网 发布:mac截图后文件在哪里 编辑:程序博客网 时间:2024/06/10 22:02

Problem 9


Special Pythagorean triplet

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.


特殊毕达哥拉斯三元组

毕达哥拉斯三元组是三个自然数a < b < c组成的集合,并满足

a2 + b2 = c2

例如,32 + 42 = 9 + 16 = 25 = 52

有且只有一个毕达哥拉斯三元组满足 a + b + c = 1000。求这个三元组的乘积abc。

package projecteuler;import org.junit.Test;public class Prj9 {/** * 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. */@Testpublic void test() {int[] abc = getABC(1000);System.out.println("a=" + abc[0] + " ,b=" + abc[1] + " ,c=" + abc[2]);System.out.println("abc = " + abc[0] * abc[1] * abc[2]);}public int[] getABC(int num) {int[] ret = new int[3];for (int i = 1; i < num; i++) {for (int j = i + 1; j < num; j++) {for (int k = j + 1; k < num; k++) {if ((i * i + j * j == k * k) && (i + j + k == 1000)) {ret[0] = i;ret[1] = j;ret[2] = k;return ret;}}}}return ret;}}


0 0