Advertisement
makispaiktis

Euler Constant γ = 0.577216

May 6th, 2020 (edited)
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #define LIMITPOWER 8
  7.  
  8. using namespace std;
  9.  
  10. // Auxiliary Functions
  11. double sumHarmonic(int a, int b){
  12.     if(a > b){
  13.         return -1000;
  14.     }
  15.     double sum = 0.0;
  16.     for(int i=a; i<b+1; i++){
  17.         sum += double(1) / double(i);
  18.     }
  19.     return sum;
  20. }
  21.  
  22. double ln(int n){
  23.     return log(n);
  24. }
  25.  
  26. double calculateEulerConstant(int n){
  27.     return sumHarmonic(1, n) - ln(n);
  28. }
  29.  
  30. int main()
  31. {
  32.     vector <double> sums = vector <double> ();
  33.     vector <double> logarithms = vector <double> ();
  34.     vector <double> euler = vector <double> ();
  35.     for(int i=0; i<LIMITPOWER; i++){
  36.         sums.push_back(sumHarmonic(1, pow(10, i+1)));
  37.         logarithms.push_back(ln(pow(10, i+1)));
  38.         euler.push_back(calculateEulerConstant(pow(10, i+1)));
  39.     }
  40.     for(int i=0; i<LIMITPOWER; i++){
  41.         cout << "~~~~~~~~ Range 1 - " << pow(10,i+1) << " ~~~~~~~~" << endl;
  42.         cout << " 1 + 1/2 + 1/3 + .... + 1/" << pow(10, i+1) - 1 << " + 1/" <<  pow(10, i+1) << " = " << sums[i] << endl;
  43.         cout << "ln(" << pow(10, i+1) << ") = " << logarithms[i] << endl;
  44.         cout << "Euler constant = " << euler[i] << endl << endl;
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement