How to resolve the algorithm Hickerson series of almost integers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

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:

  1. Importing Necessary Libraries:

    • The code imports the BigDecimal and BigInteger classes from the java.math package for high-precision decimal and integer operations.
  2. 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 and b, and then examines the fractional part of the resulting value.
  3. Calculating a and b:

    • a is calculated by raising 2 to the power of (n + 1) and multiplying it by 2. This effectively represents 2^(n+1).
    • b is calculated by dividing the factorial of n (i.e., the product of all positive integers less than or equal to n) by a.
  4. 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 a BigInteger.
    • The resulting BigInteger is then moduloed by 10 to obtain the last digit of the fractional part.
    • If this last digit is either 0 or 9, the method returns true. Otherwise, it returns false.
  5. The LN2 Constant:

    • The code defines a constant LN2 with an approximation of the natural logarithm of 2. This constant is used to compute a accurately.
  6. Main Execution:

    • In the main() method, the code iterates through integers from 1 to 17 and calls the almostInteger() method for each integer.
    • It prints the results, indicating whether each integer is "almost integer" or not.

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