# Lovelace's program for Bernouilli numbers

## Description (MathML demo)

In 1842, Ada Lovelace published a program indented to be executed on Charles Babbage's analytical machine. This program is described in her note G and allows to calculate Bernouilli numbers. For that purpose, she relied on the following series: $\frac\left\{x\right\}\left\{e^x - 1\right\} = 1 - \frac\left\{x\right\}\left\{2\right\} + \left\{B_1\frac\left\{\left\{x^2\right\}\right\}\left\{2\right\}\right\} + \left\{B_3\frac\left\{\left\{x^4\right\}\right\}\left\{4!\right\}\right\} + \left\{B_5\frac\left\{\left\{x^6\right\}\right\}\left\{6!\right\}\right\} + \left\{B_7\frac\left\{\left\{x^8\right\}\right\}\left\{8!\right\}\right\} + \left\{B_9\frac\left\{\left\{x^10\right\}\right\}\left\{10!\right\}\right\} + \cdots$ where the coefficients ${B}_{i}$'s are the Bernouilli numbers, starting at $B_1 = \frac\left\{1\right\}\left\{6\right\}$.

For any integer $n \geq 1$, she considered the following recurrence relation: $B2n-1=-(An,0+∑i=0n-2An,2i+1B2i+1)B_{2n-1} = -\left(A_{n,0} + \sum_{i=0}^{n-2} A_{n,2i+1}B_{2i+1}\right)$ where $An,0=-122n-12n+1A_{n,0} = -\frac{1}{2}\frac{2n-1}{2n+1}$ and for each integer $0 < i < n$ $A_\left\{n,2i+1\right\} = \left\{\prod_\left\{j=0\right\}^\left\{i-1\right\} \frac\left\{2n-j\right\}\left\{2+j\right\}\right\} = \frac\left\{\left\{2n\right\} \left\{\left(2n-1\right)\right\} \left\{\left(2n-2\right)\right\} \ldots \left\{\left(2n-i+1\right)\right\}\right\}\left\{2 \cdot 3 \cdot 4 \cdot \ldots \left\{\left(i+1\right)\right\}\right\}$

Finally, she noted that $An,1=2nnA_{n,1} = \frac{2n}{n}$ and used the following recurrence relation: $A_\left\{n,2\left(i+1\right)+1\right\}=A_\left\{n,2i+1\right\} \times \frac\left\{2n-i\right\}\left\{2+i\right\}$

Her famous program for $n=4$ is organized as follows:

1. $B_1, B_3, B_5$ have already been calculated and stored in result variables.
2. Operations 1-7 calculate $An,0A_{n,0}$.
3. Operations 8-12 calculate $An,1A_{n,1}$ and then $A_\left\{n,0\right\} + A_\left\{n,1\right\}B_1$.
4. Operations 13-23 are executed to calculate $A_\left\{n,3\right\}$ (using this recurrence relation) and then $A_\left\{n,0\right\} + A_\left\{n,1\right\}B_1 + A_\left\{n,3\right\}B_3$
5. Operations 13-23 are executed again to calculate $A_\left\{n,5\right\}$ (using the same recurrence relation) and then $A_\left\{n,0\right\} + A_\left\{n,1\right\}B_1 + A_\left\{n,3\right\}B_3 + A_\left\{n,5\right\}B_5$
6. Finally, operations 24-25 calculate $B_7$ (using this recurrence relation).

Additionally, she described how to generalize this algorithm using two loops (called "cycles" in the article):

• An outer one going over $n$ to successively calculate $B_1, B_3, B_5, B_7, \ldots, B_\left\{2n-1\right\}$ by repeating operations 1-25 (using this recurrence relation).
• An inner one to recursively calculate $A_\left\{n,2i+1\right\}$ (using this recurrence relation) as well as the fenced term from the above formula by repeating operations 13-23.

## JavaScript Emulation (private class fields/methods demo)

The following JavaScript program emulates Ada Lovelace's program:

Input: $n$ = ; Output ${B}_{}\approx$.

Perform invalid operations: ; Use private fields and methods: .

This program relies on a simple JavaScript class AnalyticalEngine that emulates some basic features of the analytical machine that one can guess from the article:

• Initialization: Enter an arbitrary number of variable cards and initialize them before execution.
• Execution:
• Basic operations: calculate the sum, difference, product or quotient of two variables and put the result into a third variable.
• Assignment: Copy the content of one variable into another variable.
• Comparison: Test whether two variables are equal.
• Result retrieval: Read the values of the variable cards after execution.

Isolating the analytical engine into its own class makes sure one does not "cheat" by using JavaScript features that are not supported by the analytical engine. Still, it is not obvious how some of the features were implemented, in particular:

• Handling of conditionals and loops: This is mentioned in the article but no details are provided. The program of this page relies on JavaScript's if and while.
• Access to a variable whose index is described by another variable. This is necessary to read or set the result variables (the ones where Bernouilli numbers are stored) during the execution of the two loops because the variable index depends on the loop counter.

Note that earliest versions of JavaScript do not allow developers to hide the internal structure of their classes. If you enable the "perform invalid operations" option above, the index of some result variables will be calculated by directly getting and setting the values of the internal array of variables. The program still completes correctly but this is obviously something that was unlikely to be allowed by the analytical engine, if it had ever been built.

If you browser supports private class fields and methods, you can enable the "use private fields and methods" option and verify that the invalid operations above are now prohibited.