Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- It finds the Pythagorean triples (a, b, c), with a^2 + b^2 = c^2, under the restriction a + b + c = 1000.
- The solution uses the relation b = 1000*(a - 500)/(a - 1000), which comes from solving the system of equations:
- { a^2 + b^2 = c^2,
- { a + b + c = 1000.
- Also, since a, b and c are positive integers, it is only needed to swipe the interval 1 < a < 499.
- */
- #include <stdio.h>
- #include <math.h>
- int main(){
- int a, b, c, n = 0;
- double dc, pc;
- for(a = 1; a <= 499; a++){
- b = 1000*(a - 500)/(a - 1000);
- dc = sqrt(a*a + b*b);
- modf(dc, &pc);
- c = (int)pc;
- if(c == (1000 - a - b)){
- printf("\nPythagorean Triple (%d): a = %3d, b = %3d, c = %3d\n", ++n, a, b, c);
- printf("Verifying: a+b+c = %3d, a^2+b^2 = %3d, c^2 = %3d\n", (a + b + c), (a*a + b*b), c*c);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement