document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /************************************************
  2. Programmer : Muhammad Azri bin Jasni @ Abdul Rani
  3. Program    : project euler problem 9.cpp
  4. Link       : http://projecteuler.net/problem=9
  5. *************************************************
  6. A Pythagorean triplet is a set of three natural numbers, a b c, for which,
  7.  
  8. a^2 + b^2 = c^2
  9. For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
  10.  
  11. There exists exactly one Pythagorean triplet for which a + b + c = 1000.
  12. Find the product abc.
  13. *************************************************/
  14.  
  15. /*
  16. 1*Get a possible Pythagorean triplet a b c (in natural number)
  17. 2*Check if the total sum is 1000
  18. 3*Find the product of a b c
  19. */
  20.  
  21. /*Math is challenging x.x This one I tried the 10th times ^_^;
  22. a^2 + b^2 = c^2.......(1)
  23. a + b + c = 1000......(2)
  24. ----------------
  25. From (2):
  26. c = 1000 - (a+b)......(3)
  27. ----------------
  28. (3)->(1):
  29. a^2 + b^2 = (1000 - (a+b))^2
  30. a^2 + b^2 = 1000000 - 2(a+b)(1000) + (a^2 +2ab+b^2)
  31. 0 = 1000000 - 2000(a+b) + 2ab
  32. 2000(a+b) - 2ab = 1000000
  33. 1000(a+b) - ab = 500000
  34. 1000a + 1000b - ab = 500000
  35. a(1000-b) +1000b = 500000
  36. a = (500000-1000b) / (1000-b)
  37. */
  38.  
  39. /*Round down value
  40. Returns the largest integral value that is not greater than x.
  41. double floor (      double x );
  42. float floor (       float x );
  43. long double floor ( long double x );
  44. http://www.cplusplus.com/reference/clibrary/cmath/floor/
  45.  
  46. Typecasting float/double -> int
  47. int()
  48. */
  49.  
  50. #include <iostream>
  51. #include <math.h>
  52. using namespace std;
  53.  
  54. bool check1000(double, double, double);
  55.  
  56. int main ()
  57. {
  58.     double a=0, b=0, c=0;
  59.     double product=0;
  60.     //finding the triplet
  61.     for (b=1;b<1000;b++)
  62.     {//b almost surely will be integer
  63.         //cout << "b: " << b << endl;//test
  64.         a = (500000-1000*b)/(1000-b);
  65.        // cout << "a: " << a << endl;//test
  66.         if (floor(a)==a)//rounding a to make sure it is an integer => natural number
  67.         {
  68.             c = sqrt(a*a+b*b);
  69.             if (floor(c)==c)//rounding c to make sure it is an integer => natural number
  70.             {
  71.                 //cout << "c: " << c << endl;//test
  72.                 if (check1000(a,b,c))
  73.                 {
  74.                    product = a*b*c;
  75.                    cout << "The product of " << a << ", "<< b << " and " << c << " is :" << int(product);
  76.                    break;
  77.                 }
  78.             }
  79.         }
  80.         cout << "=====" << endl;
  81.     }
  82.     cout <<endl;
  83.     //system("PAUSE");//bad way - better to open executable using command prompt or other way
  84.     return 0;
  85. }
  86.  
  87. bool check1000(double a, double b, double c)
  88. {
  89.      if (a+b+c==1000)
  90.         return true;
  91.      else
  92.          return false;
  93. }
  94. /*
  95.     //finding the triplet
  96.     for (b=1;b<1000;b++)
  97.     {
  98.         cout << "b: " << b << endl;//test
  99.         a = sqrt(500000-b*b);
  100.         cout << "a: " << a << endl;//test
  101.         if (floor(a)==a)
  102.         {
  103.             c = sqrt(a*a+b*b);
  104.             if (floor(c)==c)
  105.             {
  106.                 cout << "c: " << c << endl;//test
  107.                 if (check1000(a,b,c))
  108.                 {
  109.                    product = a*b*c;
  110.                    cout << "The product of " << a << ", "<< b << " and " << c << " is :" << product;
  111.                    break;
  112.                 }
  113.             }
  114.         }
  115. */
');