Advertisement
makispaiktis

Approaching number "e" as a division of integers

Mar 31st, 2020 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #define e 2.71828
  4. #define RELATIVEERROR 0.0005
  5. #define LIMIT 10000
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int index = -1;
  12.     for(int i=1; i<LIMIT; i++){
  13.         double product = i * e;
  14.         double error = fabs(round(product) - product) / e;
  15.         cout << i << " " << product << " " << error << endl;
  16.         if(error <= RELATIVEERROR){
  17.             index = i;
  18.             break;
  19.         }
  20.     }
  21.     // I have found the appropriate index, if this exists
  22.     if(index == -1){
  23.         cout << "I cannot find a good approach of e (relativeError <= 0.05%) with integers in the range of " << LIMIT << endl;
  24.     }
  25.     else{
  26.         cout << "A good approach of e (relativeError <= 0.05%) with integers in range of " << LIMIT << " is: " << round(index * e) << " / " << index << " = " << round(index * e) / index << endl;
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement