#include <stdio.h>
#include <math.h>

/* Naive approach using the definition
 * of γ as the difference between log(n)
 * and the Rieman sum of its derivative (1/n).
 *
 * The result is:
 *
 * approx:	0.57721 56648 77325
 * true: 	  0.57721 56649 01533
 * diff:	  0.00000 00000 24207
 *
 * The convergence is logarithmic and so
 * slow that the double precision runs out
 * at the 10th decimal digit.
 *
 */

double exact = 0.577215664901532860;

double partial_sum(size_t n) {
  double sum = 0;
  for (double k = 1; k <= n; k++) {
    sum += 1/k;
  }
  return sum - log(n);
}

int main () {
  size_t prev_n, n = 2;
  double prev_diff, diff=1;
  double approx;

  /* Compute the approximation
   * and the difference from the true value.
   * Stop when the difference starts increasing
   * due to roundoff errors.
   *
   */

  do {
    prev_n = n;
    n *= 10;
    prev_diff = diff;
    approx = partial_sum(n);
    diff = fabs(exact - approx);
    printf("n:\t%.0e\t", (double)n);
    printf("diff:\t%e\n", diff);
  } while (diff < prev_diff);

  printf("n:\t%.0e\n", (double)prev_n);
  printf("approx:\t%.15f\n", approx);
  printf("true:\t%.15f\n", exact);
  printf("diff:\t%.15f\n", prev_diff);
  printf("\t  123456789_12345\n");
}
