How to resolve the algorithm Harmonic series step by step in the C++ programming language
How to resolve the algorithm Harmonic series step by step in the C++ programming language
Table of Contents
Problem Statement
In mathematics, the n-th harmonic number is the sum of the reciprocals of the first n natural numbers: The series of harmonic numbers thus obtained is often loosely referred to as the harmonic series. Harmonic numbers are closely related to the Riemann zeta function, and roughly approximate the natural logarithm function; differing by γ (lowercase Gamma), the Euler–Mascheroni constant. The harmonic series is divergent, albeit quite slowly, and grows toward infinity.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Harmonic series step by step in the C++ programming language
This C++ program generates and prints the harmonic series. The harmonic series is the infinite sum of the reciprocals of the natural numbers:
1 + 1/2 + 1/3 + 1/4 + ...
The program uses the Boost C++ libraries to handle arbitrary-precision integer and rational number arithmetic.
The harmonic_generator
class is responsible for generating the terms of the harmonic series. The next()
method returns the next term in the series, and the reset()
method resets the generator to its initial state.
The main()
function first prints the first 20 harmonic numbers. Then, it calculates the 100th harmonic number and prints it. Finally, it finds the positions of the first terms that are greater than 1, 2, ..., 10 and prints those positions.
Here is a breakdown of the key parts of the program:
- The
harmonic_generator
class:- The
next()
method computes the next term in the harmonic series using the formula:
- The
term_ += 1 / ++n_
where term_
is the current term and n_
is the current natural number.
- The
main()
function:- The first loop prints the first 20 harmonic numbers using the
harmonic_generator
. - The second loop calculates the 100th harmonic number by repeatedly calling the
harmonic_generator
'snext()
method. - The third loop finds the positions of the first terms that are greater than 1, 2, ..., 10 by comparing the
harmonic_generator
'snext()
method's result to the natural numbern
.
- The first loop prints the first 20 harmonic numbers using the
Source code in the cpp programming language
#include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
#include <boost/multiprecision/gmp.hpp>
using integer = boost::multiprecision::mpz_int;
using rational = boost::rational<integer>;
class harmonic_generator {
public:
rational next() {
rational result = term_;
term_ += rational(1, ++n_);
return result;
}
void reset() {
n_ = 1;
term_ = 1;
}
private:
integer n_ = 1;
rational term_ = 1;
};
int main() {
std::cout << "First 20 harmonic numbers:\n";
harmonic_generator hgen;
for (int i = 1; i <= 20; ++i)
std::cout << std::setw(2) << i << ". " << hgen.next() << '\n';
rational h;
for (int i = 1; i <= 80; ++i)
h = hgen.next();
std::cout << "\n100th harmonic number: " << h << "\n\n";
int n = 1;
hgen.reset();
for (int i = 1; n <= 10; ++i) {
if (hgen.next() > n)
std::cout << "Position of first term > " << std::setw(2) << n++ << ": " << i << '\n';
}
}
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the Perl programming language
You may also check:How to resolve the algorithm Web scraping step by step in the R programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the Aime programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Java programming language
You may also check:How to resolve the algorithm Compiler/AST interpreter step by step in the C programming language