How to resolve the algorithm Bernoulli numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bernoulli numbers step by step in the Java programming language

Table of Contents

Problem Statement

Bernoulli numbers are used in some series expansions of several functions   (trigonometric, hyperbolic, gamma, etc.),   and are extremely important in number theory and analysis. Note that there are two definitions of Bernoulli numbers;   this task will be using the modern usage   (as per   The National Institute of Standards and Technology convention). The   nth   Bernoulli number is expressed as   Bn.

The Akiyama–Tanigawa algorithm for the "second Bernoulli numbers" as taken from wikipedia is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bernoulli numbers step by step in the Java programming language

The provided Java code calculates and prints the first 60 Bernoulli numbers using the Apache Commons Math library's BigFraction class. Bernoulli numbers are a sequence of rational numbers that arise in number theory and have various applications in mathematics and physics.

  • BigFraction Class:

    • BigFraction is a class that represents fractions with arbitrary-precision numerators and denominators. It provides methods for performing arithmetic operations and comparing fractions.
  • main Method:

    • The program starts with the main method. It iterates from n = 0 to n = 60 to compute and print the Bernoulli numbers.
  • Bernoulli(int n) Method:

    • This method computes the Bernoulli number with index n. It employs a recursive approach to calculate the Bernoulli numbers.

    • The method initializes an array A of BigFraction objects with a length of n + 1. It then enters a loop to fill the array with values based on the following formula:

      A[m] = 1 / (m + 1)
      for (int j = m; j >= 1; j--)
          A[j - 1] = (A[j - 1] - A[j]) * j
      
    • After filling the array, the method returns A[0], which represents the Bernoulli number B(n).

  • Output:

    • The program prints the Bernoulli numbers for indices from 0 to 60. It formats the output as B(n) = <value>, where n is the index and <value> is the calculated Bernoulli number.

Source code in the java programming language

import org.apache.commons.math3.fraction.BigFraction;

public class BernoulliNumbers {

    public static void main(String[] args) {
        for (int n = 0; n <= 60; n++) {
            BigFraction b = bernouilli(n);
            if (!b.equals(BigFraction.ZERO))
                System.out.printf("B(%-2d) = %-1s%n", n , b);
        }
    }

    static BigFraction bernouilli(int n) {
        BigFraction[] A = new BigFraction[n + 1];
        for (int m = 0; m <= n; m++) {
            A[m] = new BigFraction(1, (m + 1));
            for (int j = m; j >= 1; j--)
                A[j - 1] = (A[j - 1].subtract(A[j])).multiply(new BigFraction(j));
        }
        return A[0];
    }
}


  

You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the Rust programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Lua programming language
You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm String length step by step in the LiveCode programming language
You may also check:How to resolve the algorithm URL decoding step by step in the BaCon programming language