Advertisement
mahatma_xande

ex09lista05

Aug 28th, 2019
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. /*
  2. It finds the Pythagorean triples (a, b, c), with a^2 + b^2 = c^2, under the restriction a + b + c = 1000.
  3. The solution uses the relation b = 1000*(a - 500)/(a - 1000), which comes from solving the system of equations:
  4. { a^2 + b^2 = c^2,
  5. { a + b + c = 1000.
  6. Also, since a, b and c are positive integers, it is only needed to swipe the interval 1 < a < 499.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. int main(){
  13.  int    a, b, c, n = 0;
  14.  double  dc, pc;
  15.  
  16.  for(a = 1; a <= 499; a++){
  17.     b = 1000*(a - 500)/(a - 1000);
  18.     dc = sqrt(a*a + b*b);
  19.  
  20.     modf(dc, &pc);
  21.     c = (int)pc;
  22.  
  23.     if(c == (1000 - a - b)){
  24.         printf("\nPythagorean Triple (%d): a = %3d, b = %3d, c = %3d\n", ++n, a, b, c);
  25.         printf("Verifying: a+b+c = %3d, a^2+b^2 = %3d, c^2 = %3d\n", (a + b + c), (a*a + b*b), c*c);
  26.     }
  27.  }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement