Advertisement
makispaiktis

Project Euler 454 - Diophantine Reciprocals

Apr 9th, 2020 (edited)
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. /*
  2. https://projecteuler.net/problem=454
  3. In the following equation x, y, and n are positive integers.
  4.  
  5. (1/x) + (1/y) = (1/n)
  6. For a limit L we define F(L) as the number of solutions which satisfy x < y ≤ L.
  7.  
  8. We can verify that F(15) = 4 and F(1000) = 1069.
  9. Find F(10^12).
  10. */
  11.  
  12.  
  13. #include <iostream>
  14. #include <stdlib.h>
  15. #include <math.h>
  16.  
  17. using namespace std;
  18.  
  19. int findNumOfSolutions(int limit){
  20.     int counter = 0;
  21.     for(int x=1; x<limit; x++){
  22.         for(int y=x+1; y<limit+1; y++){
  23.             double diairesi = double(x * y) / double(x + y);
  24.             // cout << "x = " << x << ", y = " << y << " ---> " << diairesi << " " << int(diairesi) << endl;
  25.             if(diairesi == int(diairesi)){
  26.                 counter++;
  27.             }
  28.         }
  29.     }
  30.     return counter;
  31. }
  32.  
  33. int main()
  34. {
  35.     cout << "I look for pairs of positive integers x, y and n, that satisfy the following condition:\n";
  36.     cout << " (1/x) + (1/y) = (1/n) <==> (x*y) / (x+y) = n = integer, where: x < y <=L, L = a positive integer limit\n\n";
  37.     cout << "Number of solutions from 1 to 15 = F(15) = " << findNumOfSolutions(15) << endl;
  38.     cout << "Number of solutions from 1 to 1000 = F(1000) = " << findNumOfSolutions(1000) << endl;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement