How to resolve the algorithm Hickerson series of almost integers step by step in the Java programming language
How to resolve the algorithm Hickerson series of almost integers step by step in the Java programming language
Table of Contents
Problem Statement
The following function, due to D. Hickerson, is said to generate "Almost integers" by the "Almost Integer" page of Wolfram MathWorld, (December 31 2013). (See formula numbered 51.)
The function is:
h ( n )
n !
2 ( ln
2
)
n + 1
{\displaystyle h(n)={\operatorname {n} ! \over 2(\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
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:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hickerson series of almost integers step by step in the Java programming language
Purpose: The provided Java code determines whether a given integer is "almost integer" based on a mathematical property related to the binary logarithm.
Explanation:
-
Importing Necessary Libraries:
- The code imports the
BigDecimal
andBigInteger
classes from thejava.math
package for high-precision decimal and integer operations.
- The code imports the
-
The
almostInteger()
Method:- The method takes an integer
n
as an input and checks if it satisfies a certain mathematical condition. - It calculates two decimal values,
a
andb
, and then examines the fractional part of the resulting value.
- The method takes an integer
-
Calculating
a
andb
:a
is calculated by raising 2 to the power of(n + 1)
and multiplying it by 2. This effectively represents2^(n+1)
.b
is calculated by dividing the factorial ofn
(i.e., the product of all positive integers less than or equal ton
) bya
.
-
Checking the Fractional Part:
- The fractional part of
b
is extracted by moving the decimal point one place to the right and converting it to aBigInteger
. - The resulting
BigInteger
is then moduloed by 10 to obtain the last digit of the fractional part. - If this last digit is either
0
or9
, the method returnstrue
. Otherwise, it returnsfalse
.
- The fractional part of
-
The
LN2
Constant:- The code defines a constant
LN2
with an approximation of the natural logarithm of 2. This constant is used to computea
accurately.
- The code defines a constant
-
Main Execution:
- In the
main()
method, the code iterates through integers from 1 to 17 and calls thealmostInteger()
method for each integer. - It prints the results, indicating whether each integer is "almost integer" or not.
- In the
Mathematical Background:
The code's logic stems from a mathematical property that states:
2^(n+1) ≈ n! (mod 10)
or more specifically:
n! / 2^(n+1) ≈ 0.5 (mod 1)
This property implies that the fractional part of the decimal value n! / 2^(n+1)
is approximately 0.5. Thus, if the fractional part of this value is close to 0 or 1 (i.e., its last digit is 0 or 9), it indicates that the integer is "almost integer."
Source code in the java programming language
import java.math.*;
public class Hickerson {
final static String LN2 = "0.693147180559945309417232121458";
public static void main(String[] args) {
for (int n = 1; n <= 17; n++)
System.out.printf("%2s is almost integer: %s%n", n, almostInteger(n));
}
static boolean almostInteger(int n) {
BigDecimal a = new BigDecimal(LN2);
a = a.pow(n + 1).multiply(BigDecimal.valueOf(2));
long f = n;
while (--n > 1)
f *= n;
BigDecimal b = new BigDecimal(f);
b = b.divide(a, MathContext.DECIMAL128);
BigInteger c = b.movePointRight(1).toBigInteger().mod(BigInteger.TEN);
return c.toString().matches("0|9");
}
}
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Character codes step by step in the Batch File programming language
You may also check:How to resolve the algorithm Almost prime step by step in the F# programming language