Home > Programming, Technology > Pythagorean triplet

Pythagorean triplet


Find the only Pythagorean triplet (a, b, c), for which a + b + c = 1000?

Here Pythagorean triplet means a2 +b2=c2

In order to find the triplet which satisfies the given criteria, we have to consider  the following rules

a+b+c=1000

hence c=1000-a-b

bounds:

3<a<1000/3   (a<b<c)

a+1<=b<1000/2

Here is the working java code
[soucecode language=”java”]

/**
*
* @author Vasanth Raja Chittampally
*/

public class Main {

public static void main(String[] args) {

out:for (int i = 1; i <1000/3; i++) {
for (int j = 1; j < 1000/2; j++) {
int k=(1000-i-j);
if(i*i+j*j==k*k)
{
if(i+j+k==1000)
{
System.out.println(i+” “+j+” “+k);
System.out.println(“”+(i*j*k));
break out;
}
}

}

}
}

}

[/sourcecode]

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: