-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*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 400, with side1<= side2. | ||
Use a triple-nested for loop that simply tries all possibilities. This is an example of the “brute-force” approach. | ||
Your program should print each triple on a separate line, and the total number of triples at the end*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
|
||
int main() | ||
{ /* program takes all pythagorean triplets | ||
from 0 to 400 and prints them */ | ||
int side1,side2,side3,sum=0; | ||
// 3 for loops are iterated through from 0 to 400 in order to help find the pytagorean triplets | ||
for(side1=1;side1<=400;side1++){ | ||
for(side2=1;side2<=400;side2++){ | ||
for(side3=1;side3<=400;side3++){ | ||
if(side1*side1+side2*side2 == side3*side3 && side1<=side2){ /* if side1^2 + side2^2 = s | ||
ide3^2 (and they are equal to an integer) then sum will increase by 1 | ||
and the 3 sides will be printed*/ | ||
sum +=1; | ||
printf("%d,%d,%d\n",side1,side2,side3); // prints all 3 side | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
printf("%d",sum); | ||
return 0; | ||
} | ||
|