Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a java to make a Pythagorean?

Status
Not open for further replies.

kickerboy123

Technical User
Nov 13, 2003
12
US
I am learn java now, Can anyone give me a tip how to make a pythagorean triples by using Java ?
like using For or do.....while statement?

Thanks

Lai
 
Hi

With brute force, up to 2006 :
Code:
[b]for[/b] ([b]int[/b] i=1;i<=[green][i]2006[/i][/green];i++)
[b]for[/b] ([b]int[/b] j=1;j<=i;j++)
[b]if[/b] (Math.sqrt(i*i+j*j)==Math.round(Math.sqrt(i*i+j*j)))
System.out.println(j+[i]", "[/i]+i+[i]", "[/i]+Math.sqrt(i*i+j*j));

Feherke.
 
I'm not sure that will work, I think you need to stablish a treshold:

Code:
for (int i=1;i<=2006;i++)
  for (int j=1;j<=i;j++)
    if (Math.sqrt(i*i+j*j)-Math.round(Math.sqrt(i*i+j*j))<0.00001)
System.out.println(j+", "+i+", "+Math.sqrt(i*i+j*j));

Cheers,
Dian
 
Hi

Hmm... You may be right. But as far as I observed during the test, seems that Java has no problem with this calculation.

But I think, your suggestion would look better this way :
Code:
if ([red]Math.abs([/red]Math.sqrt(i*i+j*j)-Math.round(Math.sqrt(i*i+j*j))[red])[/red]<0.00001)
System.out.println(j+", "+i+", "+Math.sqrt(i*i+j*j));

Feherke.
 
Thanks Feherke and Dian, i will try both program tonight ,

thanks a lot

Lai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top