# Web Developer

## Java – Pythagorean Triples

### Mountain State University Java AssisgnmentJava How to Program: Exercise 4.21 page 165

#### 4.21 (Pythagorean Triples) 4.21 (Pythagorean Triples – Source Code)

/*
Pythagorean Triple
A right triangle can have sides whose lengths are all integers.
The set of three integer values for the lengths of the sides of a right triangle
is called a Pythagorean Triple. The lengths of three sides must satisfy the relationship that the sum of the
squares of two of the sides is equal to the square of the hypotenuse.Write an application that displays a table of Pythagorean triples for side1,
side2, and hypotenuse,all no longer than 500. Use a triple-nested for loop that tries all possibilities. This method is an example of “brute force” computing. You’ll learn in more advanced computer science courses that for many interesting
problems there is no known algorithmic approach other than sheer brute force.
Algebra equation for a PythagoreanTriple is:(a*a) + (b*b) = (c*c)
*/
/*
This program will include a counter that does the following(using base3 for
example):
111, 112, 113,
121, 122, 123,
131, 132, 133,
211, 212, 213,
221, 222, 223,
231, 232, 233,
311, 312, 313,
321, 322, 323,
331, 332, 333
Program will need a counter capable of counting 3 lines of base 500
Each time counter is incremented a test for pytha = hypo must be implemented.
*/
import java.util.Scanner; // initialize scanner
public class PythagoreanTriple
{
public static void main( String[] args )
{

for ( int a = 1; a <= 500; a++ )
{
for ( int b = 1; b <= 500; b++ )
{
for ( int c = 1; c <= 500; c++ )
{

if( (a * a) + (b * b) == (c * c) )
{
System.out.printf( “%d, %d, %d”, a, b, c );
System.out.println();
} // end if
} // end 3rd loop inside of 2nd loop
} // end 2nd loop inside of first loop
} // end first loop

} // end method main

} // end class PythagoreanTriple