PROBLEM
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.
ANSWER
31875000
JAVA CODE
public class Problem9 {
public static void main(String[]args){
int a=3;
int b=4;
int c=1000-(b+a);
int ans=0;
boolean find=false;
for (int i = 4; i < 496; i++) {
for (int j = 5; j < 499; j++) {
if((Math.pow(a,2)+Math.pow(b,2))==Math.pow(c,2)){
ans=a*b*c;
System.out.println("a="+a+"b="+b+"c="+c);
find=true;
break;
}
b=j;
c=1000-(b+a);
}
a=i;
if(find)
break;
}
System.out.println(ans);
}
}
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.
ANSWER
31875000
JAVA CODE
public class Problem9 {
public static void main(String[]args){
int a=3;
int b=4;
int c=1000-(b+a);
int ans=0;
boolean find=false;
for (int i = 4; i < 496; i++) {
for (int j = 5; j < 499; j++) {
if((Math.pow(a,2)+Math.pow(b,2))==Math.pow(c,2)){
ans=a*b*c;
System.out.println("a="+a+"b="+b+"c="+c);
find=true;
break;
}
b=j;
c=1000-(b+a);
}
a=i;
if(find)
break;
}
System.out.println(ans);
}
}
0 comments:
Post a Comment