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:
1 public class Main 2 { 3 public static void main(String[] args) 4 { 5 System.out.println(run(1000)); 6 } 7 public static String run(int n) 8 { 9 String a=""; 10 for(int i=1;i<n;i++) 11 { 12 for(int j=0;j<i;j++) 13 { 14 for(int s=0;s<j;s++) 15 { 16 if(s*s+j*j==i*i) 17 { 18 if(s+j+i == 1000) 19 { 20 a =String.valueOf(s*j*i); 21 } 22 } 23 } 24 } 25 } 26 return a; 27 } 28 }
时间效率:280毫秒。