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,形如
举个例子,32 + 42 = 9 + 16 = 25 = 52.
现在存在一个毕达哥拉斯三元数组,它满足 a + b + c = 1000.毕达哥拉斯三元数组的数值乘积。
第一次code:
public class Main { public static void main(String[] args) { System.out.println(run(1000)); } public static String run(int n) { String a=""; for(int i=1;i<n;i++) { for(int j=0;j<i;j++) { for(int s=0;s<j;s++) { if(s*s+j*j==i*i) { if(s+j+i == 1000) { a =String.valueOf(s*j*i); } } } } } return a; } }
时间效率:280毫秒。