C++: Hickerson Series of Almost Integers

Bjarne-stroustrup
 

The following function, due to D Hickerson, is said to generate “Almost integers” by the “Almost Integer” page of Wolfram Mathworld. (December 31 2013).

The function is:

h(n) = {\operatorname{n}!\over2(\ln{2})^{n+1}}

It is said to produce “almost integers” for n between 1 and 17. The purpose of the task is to verify this assertion.

Assume that an “almost integer” has either a nine or a zero as its first digit after the decimal point of its decimal string representation

The task is to calculate all values of the function checking and stating which are “almost integers”.

Note: Use extended/arbitrary precision numbers in your calculation if necessary to ensure you have adequate precision of results as for example:

   h(18) = 3385534663256845326.39...

#include <iostream>
#include <iomanip>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/constants/constants.hpp>
typedef boost::multiprecision::cpp_dec_float_50 decfloat;

int main()
{
	const decfloat ln_two = boost::math::constants::ln_two<decfloat>();
	decfloat numerator = 1, denominator = ln_two;

	for(int n = 1; n <= 17; n++) {
		decfloat h = (numerator *= n) / (denominator *= ln_two) / 2;
		decfloat tenths_dig = floor((h - floor(h)) * 10);
		std::cout << "h(" << std::setw(2) << n << ") = " << std::setw(25) << std::fixed << h << 
		(tenths_dig == 0 || tenths_dig == 9 ? " is " : " is NOT ") << "an almost-integer.\n";
	}
}
Output:
h( 1) =                  1.040684 is an almost-integer.
h( 2) =                  3.002781 is an almost-integer.
h( 3) =                 12.996291 is an almost-integer.
h( 4) =                 74.998735 is an almost-integer.
h( 5) =                541.001519 is an almost-integer.
h( 6) =               4683.001247 is an almost-integer.
h( 7) =              47292.998731 is an almost-integer.
h( 8) =             545834.997907 is an almost-integer.
h( 9) =            7087261.001623 is an almost-integer.
h(10) =          102247563.005271 is an almost-integer.
h(11) =         1622632572.997550 is an almost-integer.
h(12) =        28091567594.981572 is an almost-integer.
h(13) =       526858348381.001248 is an almost-integer.
h(14) =     10641342970443.084532 is an almost-integer.
h(15) =    230283190977853.037436 is an almost-integer.
h(16) =   5315654681981354.513077 is NOT an almost-integer.
h(17) = 130370767029135900.457985 is NOT an almost-integer.

SOURCE

Content is available under GNU Free Documentation License 1.2.