// harmonisch.cc
// Demonstration von beliebig grossen rationale und Fliesskommazahlen
// Compilation mit:  g++ harmonisch.cc -lgmpxx -lgmp
#include <gmpxx.h>
#include <float.h>
#include <iostream>
#include <iomanip>
using namespace std;

typedef mpq_class Rat;
typedef mpf_class Float;


Rat Harmonic(unsigned int n) {
  Rat r = 0;
  for(int i=1; i <=n; i++) {
    r += Rat(1,i);
  }
  return r;
}

Rat Harmonic1(unsigned int n) {
  Rat r = 0;
  for(int i=n; i >= 1; i--) {
    r += Rat(1,i);
  }
  return r;
}

// Die Summe ist fuer long double abhaengig von der Summationsreihenfolge:
long double harmonic(unsigned int n) {
    long double r = 0;
    for(int i = 1; i <= n; i++) {
      r += 1/(long double)i;
    }
    return r;
}

long double harmonic1(unsigned int n) {
    long double r = 0;
    for(int i = n; i >= 1; i--) {
      r += 1/(long double)i;
    }
    return r;
}

int main() {
  Rat h,k;
  unsigned int n;
  cout << "Eingabe : ";
  cin  >> n;
  cout << setprecision(23);
  h = Harmonic( n);
  cout << "Harmonic ("  << n << ") = "  << (Float)h;
  if (n<100) cout << " = " << h         << "\n";
  else       cout << " = " << "<Bruch>" << "\n";
  k = Harmonic1( n);
  cout << "Harmonic1("  << n << ") = "  << (Float)k;
  if (n<100) cout << " = " << k         << "\n";
  else       cout << " = " << "<Bruch>" << "\n";

  cout << "harmonic ("  << n << ") = " << harmonic( n) << "\n";
  cout << "harmonic1("  << n << ") = " << harmonic1(n) << "\n";
}

/*
Eingabe : 1000000
Harmonic (1000000) = 14.3927267228657236314
Harmonic1(1000000) = 14.3927267228657236314
harmonic (1000000) = 14.392726722865723355295
harmonic1(1000000) = 14.392726722865723646728
*/
