Jump to content

Pythagorean Triple

- - - - -

  • This topic is locked This topic is locked
15 replies to this topic

#1
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
I'm trying to make a program that outputs a list of all Pythagorean triples, and all 3 integers shouldn't be larger than 500.

public class pythagorean

{

	public static void main(String[] args)

	{

		int a, b, c;


		for (a = 1; a <= 500; a++)

		{

			for (b = 1; a <= 500; b++)

			{

				for (c = 1; c <= 500; c++)

				{

					if (Math.pow(a,2) + Math.pow(b,2) == Math.pow(c,2))

					{

						System.out.printf("%d   %d   %d\n", a, b, c);

					}

				}

			}

		}

	}

}

For some reason, nothing appears on the output window:
Attached File  cmd.jpg   32.91K   67 downloads

EDIT: Ok, I edited the program a little bit. Instead of using Math.pow(), I just multiplied the variables to themselves. Here's the code:

public class pythagorean

{

	public static void main(String[] args)

	{

		int a, b, c;


		for (a = 1; a <= 500; a++)

		{

			for (b = 1; a <= 500; b++)

			{

				for (c = 1; c <= 500; c++)

				{

					if ((a * a) + (b * b) == (c * c))

					{

						System.out.println(a + "  " + b + "  " + c);

					}

				}

			}

		}

	}

}

And now this happened:
Attached File  cmd2.jpg   65.02K   90 downloads

EDIT 2: Oooh, now I saw the error. I mistyped the second for loop condition. Forget the whole thing, I got it now. Sorry about wasting your time. :P

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
This should be a little more efficient, since it has 2 nested loops instead of 3:

int main()

{

	int i;


	for(i=0;i<100;i++){

		printIntTriangles(i);

	}


	return 0;

}


//prints the triangles that have perimeter p

void printIntTriangles(int p){

	int a=p/2-1, b;


	for(b=1;b<a;b++){

		if( !((p*(p-2*b))%(2*(p-b))) ){

			a=(p*(p-2*b))/(2*(p-b));

			printf("perimeter=%d, i=%d, c1=%d, c2=%d\n", p, p-a-b, a, b);

		}

	}

}


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It still has 3, it just has 1 in the main now.

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
One in the main, one in printIntTriangles, that's two :D

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
but 1+1= thr... ow wait .. :D

I read the if as a for :glare:

#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
yeah I imagined that ;)

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Now, to get back on topic. You can also use Euclid's formula. Looks extremely simple and it works. Requires 2 random integers to start with (so also 2 loops).
And then you just calculate the 3 numbers with those 2.
Posted Image
There you calculate a, b, and c with the 2 random integers n and m where a²+b²=c²


In your own code you can also add a break; in the 3th loop to make it a bit faster:

for (c = 1; c <= 500; c++)

				{

					if ((a * a) + (b * b) == (c * c))

					{

						System.out.println(a + "  " + b + "  " + c);

					}

                                        if (((a * a) + (b * b)) < (c * c))

					{

						break;

					}

				}



#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I knew there had to be a more efficient way to handle this :)

Here's a cool wiki entry:
Formulas for generating Pythagorean triples - Wikipedia, the free encyclopedia

I won't have time to look through it but it's very interesting.

#9
hacked

hacked

    Newbie

  • Members
  • Pip
  • 4 posts
Hey - If your going to steal my code and claim you wrote it, at the very least you could link to my site.

You did not write this code.

#10
hacked

hacked

    Newbie

  • Members
  • Pip
  • 4 posts
The original code was written by Volunteer Web Developer | Volunteer Web Designer

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
@hacked: your site does not have date/time stamps. That makes it a little difficult to verify who copied from whom. All that is certain, currently, is that your copy is on page 8 of your blog.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts

hacked said:

Hey - If your going to steal my code and claim you wrote it, at the very least you could link to my site.

You did not write this code.

Who, me?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users