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

Published on 12 May 2024 09:40 PM

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

The provided Python code defines two functions, bernoulli and bernoulli2, to compute Bernoulli numbers. Bernoulli numbers are a sequence of rational numbers that appear in various mathematical contexts, such as number theory and probability theory.

Function 1: bernoulli

  • Input: n, a non-negative integer representing the index of the Bernoulli number to be computed.
  • Output: The nth Bernoulli number as a fraction object from the fractions module.

Implementation:

  • Create a list A of length n+1 and initialize each element to 1/(m+1), where m ranges from 0 to n.
  • For each m from 0 to n, we use a nested loop to update the elements of A according to the recurrence relation for Bernoulli numbers: A[j-1] = j * (A[j-1] - A[j]) for j from m to 1.
  • The first element of the list A[0] contains the value of the nth Bernoulli number.

Function 2: bernoulli2

  • Input: None (it's a generator function).
  • Output: A sequence of Bernoulli numbers as fraction objects.

Implementation:

  • Initialize an empty list A and an integer variable m to 0.
  • In a loop, repeatedly perform the following steps:
    • Append 1/(m+1) to A.
    • Update the elements of A using the same recurrence relation as in bernoulli.
    • Yield the first element of A, which is the mth Bernoulli number.
    • Increment m by 1.

Usage:

The code includes the following usage examples:

  • Compute and print the first 61 Bernoulli numbers using both methods bernoulli and bernoulli2.
  • The output is formatted to align the numerator and denominator for visual consistency.

Explanation:

Bernoulli numbers have various applications in mathematical areas like number theory, probability theory, and combinatorics. Their closed-form expression can get quite complex, but the provided functions efficiently calculate them using the recurrence relation.

Source code in the python programming language

from fractions import Fraction as Fr

def bernoulli(n):
    A = [0] * (n+1)
    for m in range(n+1):
        A[m] = Fr(1, m+1)
        for j in range(m, 0, -1):
          A[j-1] = j*(A[j-1] - A[j])
    return A[0] # (which is Bn)

bn = [(i, bernoulli(i)) for i in range(61)]
bn = [(i, b) for i,b in bn if b]
width = max(len(str(b.numerator)) for i,b in bn)
for i,b in bn:
    print('B(%2i) = %*i/%i' % (i, width, b.numerator, b.denominator))


def bernoulli2():
    A, m = [], 0
    while True:
        A.append(Fr(1, m+1))
        for j in range(m, 0, -1):
          A[j-1] = j*(A[j-1] - A[j])
        yield A[0] # (which is Bm)
        m += 1

bn2 = [ix for ix in zip(range(61), bernoulli2())]
bn2 = [(i, b) for i,b in bn2 if b]
width = max(len(str(b.numerator)) for i,b in bn2)
for i,b in bn2:
    print('B(%2i) = %*i/%i' % (i, width, b.numerator, b.denominator))


  

You may also check:How to resolve the algorithm RPG attributes generator step by step in the Plain English programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Lua programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Ada programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the Kotlin programming language