Deitel & Deitel, C How to Program 6th Edition, Exercise 4.27

/*4.27 (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three
integer values for the sides of a right triangle is called a Pythagorean triple. These 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. Find all Pythagorean triples for side1, side2, and the hypotenuse all no larger than 500.
Use a triple-nested for loop that simply tries all possibilities. This is an example of “brute-force”
computing. It’s not aesthetically pleasing to many people. But there are many reasons why these
techniques are important. First, with computing power increasing at such a phenomenal pace, solutions
that would have taken years or even centuries of computer time to produce with the technology
of just a few years ago can now be produced in hours, minutes or even seconds. Recent
microprocessor chips can process a billion instructions per second! Second, as you’ll learn in more
advanced computer science courses, there are large numbers of interesting problems for which there
is no known algorithmic approach other than sheer brute force. We investigate many kinds of problem-
solving methodologies in this book. We’ll consider many brute-force approaches to various interesting
problems.*/

#include <stdio.h>

int main() {
int a, b, c, max;

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

if (a >= b && a >= c)
max = a;
else if (b >= c)
max = b;
else
max = c;

if (max == a)
if (b*b + c * c == a * a)
printf("%d,%d,%d is a pythagorean triple\n", a, b, c);
if (max == b)
if (a*a + c * c == b * b)
printf("%d,%d,%d is a pythagorean triple\n", a, b, c);
if (max == c)
if (b*b + a * a == c * c)
printf("%d,%d,%d is a pythagorean triple\n", a, b, c);


}
}
}

return 0;
}

Comments

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)