banovski

Project Euler, Problem #6, C

Dec 14th, 2021 (edited)
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. /* Find the difference between the sum of the squares of the first one
  2. hundred natural numbers and the square of the sum. */
  3.  
  4. /* Solution #1 */
  5.  
  6. #include <stdio.h>
  7.  
  8. int main()
  9. {
  10.      int i;
  11.      int natNumSum = 0, squareNumSum = 0;
  12.  
  13.      for(i = 1; i < 101; i++)
  14.           natNumSum += i;
  15.      
  16.      for(i = 1; i < 101; i++)
  17.           squareNumSum += i * i;
  18.  
  19.      printf("%d\n", natNumSum * natNumSum - squareNumSum);
  20.      return(0);
  21. }
  22.  
  23. /* 25164150 */
  24.  
  25.  
  26. /* Solution #2 */
  27.  
  28. #include <stdio.h>
  29. #include <math.h>
  30.  
  31. int main()
  32. {
  33.      int i;
  34.      double natNumSum = 0;
  35.      double squareNumSum = 0;
  36.  
  37.      for(i = 1; i < 101; i++)
  38.           natNumSum += i;
  39.      
  40.      for(i = 1; i < 101; i++)
  41.           squareNumSum += pow(i, 2);
  42.  
  43.      printf("%.0f\n", pow(natNumSum, 2) - squareNumSum);
  44.      return(0);
  45. }
  46.  
  47. /* 25164150 */
Add Comment
Please, Sign In to add comment