Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************
- Programmer : Muhammad Azri bin Jasni @ Abdul Rani
- Program : project euler problem 9.cpp
- Link : http://projecteuler.net/problem=9
- *************************************************
- A Pythagorean triplet is a set of three natural numbers, a b c, for which,
- a^2 + b^2 = c^2
- For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
- There exists exactly one Pythagorean triplet for which a + b + c = 1000.
- Find the product abc.
- *************************************************/
- /*
- 1*Get a possible Pythagorean triplet a b c (in natural number)
- 2*Check if the total sum is 1000
- 3*Find the product of a b c
- */
- /*Math is challenging x.x This one I tried the 10th times ^_^;
- a^2 + b^2 = c^2.......(1)
- a + b + c = 1000......(2)
- ----------------
- From (2):
- c = 1000 - (a+b)......(3)
- ----------------
- (3)->(1):
- a^2 + b^2 = (1000 - (a+b))^2
- a^2 + b^2 = 1000000 - 2(a+b)(1000) + (a^2 +2ab+b^2)
- 0 = 1000000 - 2000(a+b) + 2ab
- 2000(a+b) - 2ab = 1000000
- 1000(a+b) - ab = 500000
- 1000a + 1000b - ab = 500000
- a(1000-b) +1000b = 500000
- a = (500000-1000b) / (1000-b)
- */
- /*Round down value
- Returns the largest integral value that is not greater than x.
- double floor ( double x );
- float floor ( float x );
- long double floor ( long double x );
- http://www.cplusplus.com/reference/clibrary/cmath/floor/
- Typecasting float/double -> int
- int()
- */
- #include <iostream>
- #include <math.h>
- using namespace std;
- bool check1000(double, double, double);
- int main ()
- {
- double a=0, b=0, c=0;
- double product=0;
- //finding the triplet
- for (b=1;b<1000;b++)
- {//b almost surely will be integer
- //cout << "b: " << b << endl;//test
- a = (500000-1000*b)/(1000-b);
- // cout << "a: " << a << endl;//test
- if (floor(a)==a)//rounding a to make sure it is an integer => natural number
- {
- c = sqrt(a*a+b*b);
- if (floor(c)==c)//rounding c to make sure it is an integer => natural number
- {
- //cout << "c: " << c << endl;//test
- if (check1000(a,b,c))
- {
- product = a*b*c;
- cout << "The product of " << a << ", "<< b << " and " << c << " is :" << int(product);
- break;
- }
- }
- }
- cout << "=====" << endl;
- }
- cout <<endl;
- //system("PAUSE");//bad way - better to open executable using command prompt or other way
- return 0;
- }
- bool check1000(double a, double b, double c)
- {
- if (a+b+c==1000)
- return true;
- else
- return false;
- }
- /*
- //finding the triplet
- for (b=1;b<1000;b++)
- {
- cout << "b: " << b << endl;//test
- a = sqrt(500000-b*b);
- cout << "a: " << a << endl;//test
- if (floor(a)==a)
- {
- c = sqrt(a*a+b*b);
- if (floor(c)==c)
- {
- cout << "c: " << c << endl;//test
- if (check1000(a,b,c))
- {
- product = a*b*c;
- cout << "The product of " << a << ", "<< b << " and " << c << " is :" << product;
- break;
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement