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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Bernoulli numbers step by step in the Julia 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 Julia programming language

This code generates and displays the Bernoulli numbers up to a given index n. The Bernoulli numbers are a sequence of rational numbers that arise in various mathematical contexts, such as number theory, combinatorics, and probability theory.

Here's a detailed explanation of the code:

  1. bernoulli(n) Function:

    • This function calculates the n-th Bernoulli number.
    • It uses the recurrence relation to compute the Bernoulli numbers.
    • It takes an integer n as input and returns the corresponding Bernoulli number as a Rational{BigInt}.
  2. display(n) Function:

    • This function displays the Bernoulli numbers up to index n.
    • It uses the bernoulli function to compute the numbers.
    • It formats and prints the numbers in a readable way.
  3. Displaying the Bernoulli Numbers:

    • The display function is called with n=60 to display the first 60 Bernoulli numbers.
    • The output includes both the index and the value of each number, formatted as a fraction.
  4. Alternative Approach with BernoulliList(len):

    • This function computes the list of Bernoulli numbers up to a given length len in a more efficient way.
    • It uses the same recurrence relation as the bernoulli function but stores the intermediate results in vectors.
    • It returns a vector of Rational{BigInt} containing the Bernoulli numbers.
  5. Displaying the Bernoulli Numbers Efficiently:

    • After computing the Bernoulli list, the code iterates over the list and prints only the odd-indexed numbers.
    • This is because the Bernoulli numbers with even indices are all zero.

In summary, this Julia code computes and displays the Bernoulli numbers using two functions: bernoulli for computing individual numbers and BernoulliList for computing a list of numbers efficiently. It then formats and prints the numbers for easy readability.

Source code in the julia programming language

function bernoulli(n)
    A = Vector{Rational{BigInt}}(undef, n + 1)
    for m = 0 : n
        A[m + 1] = 1 // (m + 1)
        for j = m : -1 : 1
            A[j] = j * (A[j] - A[j + 1])
        end
    end
    return A[1]
end

function display(n)
    B = map(bernoulli, 0 : n)
    pad = mapreduce(x -> ndigits(numerator(x)) + Int(x < 0), max, B)
    argdigits = ndigits(n)
    for i = 0 : n
        if numerator(B[i + 1]) & 1 == 1
            println(
                "B(", lpad(i, argdigits), ") = ",
                lpad(numerator(B[i + 1]), pad), " / ", denominator(B[i + 1])
            )
        end
    end
end

display(60)

# Alternative: Following the comment in the Perl section it is much more efficient
# to compute the list of numbers instead of one number after the other.

function BernoulliList(len)
    A = Vector{Rational{BigInt}}(undef, len + 1)
    B = similar(A)
    for n in 0 : len
        A[n + 1] = 1 // (n + 1)
        for j = n : -1 : 1
            A[j] = j * (A[j] - A[j + 1])
        end
        B[n + 1] =  A[1]
    end
    return B
end

for (n, b) in enumerate(BernoulliList(60))
    isodd(numerator(b)) && println("B($(n-1)) = $b")
end


  

You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the Factor programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the J programming language
You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm 100 doors step by step in the langur programming language