Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- https://projecteuler.net/problem=454
- In the following equation x, y, and n are positive integers.
- (1/x) + (1/y) = (1/n)
- For a limit L we define F(L) as the number of solutions which satisfy x < y ≤ L.
- We can verify that F(15) = 4 and F(1000) = 1069.
- Find F(10^12).
- */
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- using namespace std;
- int findNumOfSolutions(int limit){
- int counter = 0;
- for(int x=1; x<limit; x++){
- for(int y=x+1; y<limit+1; y++){
- double diairesi = double(x * y) / double(x + y);
- // cout << "x = " << x << ", y = " << y << " ---> " << diairesi << " " << int(diairesi) << endl;
- if(diairesi == int(diairesi)){
- counter++;
- }
- }
- }
- return counter;
- }
- int main()
- {
- cout << "I look for pairs of positive integers x, y and n, that satisfy the following condition:\n";
- cout << " (1/x) + (1/y) = (1/n) <==> (x*y) / (x+y) = n = integer, where: x < y <=L, L = a positive integer limit\n\n";
- cout << "Number of solutions from 1 to 15 = F(15) = " << findNumOfSolutions(15) << endl;
- cout << "Number of solutions from 1 to 1000 = F(1000) = " << findNumOfSolutions(1000) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement