Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Program: harmonic.cpp // Compute the n-th harmonic number in two ways. #include int main() { // Input std::cout << "Compute H_n for n =? "; unsigned int n; std::cin >> n; // Forward sum float fs = 0; for (unsigned int i = 1; i <= n; ++i) fs += 1.0f / i; // Backward sum float bs = 0; for (unsigned int i = n; i >= 1; --i) bs += 1.0f / i; // Output std::cout << "Forward sum = " << fs << "\n" << "Backward sum = " << bs << "\n"; return 0; }