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: $$\backslash frac\{x\}\{e^x\; -\; 1\}\; =\; 1\; -\; \backslash frac\{x\}\{2\}\; +\; \{B\_1\backslash frac\{\{x^2\}\}\{2\}\}\; +\; \{B\_3\backslash frac\{\{x^4\}\}\{4!\}\}\; +\; \{B\_5\backslash frac\{\{x^6\}\}\{6!\}\}\; +\; \{B\_7\backslash frac\{\{x^8\}\}\{8!\}\}\; +\; \{B\_9\backslash frac\{\{x^10\}\}\{10!\}\}\; +\; \backslash cdots$$ where the coefficients ${B}_{i}$'s are the Bernouilli numbers, starting at $B\_1\; =\; \backslash frac\{1\}\{6\}$.
For any integer $n\; \backslash geq\; 1$, she considered the following recurrence relation: $$B\_\{2n-1\}\; =\; -\backslash left(A\_\{n,0\}\; +\; \backslash sum\_\{i=0\}^\{n-2\}\; A\_\{n,2i+1\}B\_\{2i+1\}\backslash right)$$ where $A\_\{n,0\}\; =\; -\backslash frac\{1\}\{2\}\backslash frac\{2n-1\}\{2n+1\}$ and for each integer $0\; <\; i\; <\; n$ $$A\_\{n,2i+1\}\; =\; \{\backslash prod\_\{j=0\}^\{i-1\}\; \backslash frac\{2n-j\}\{2+j\}\}\; =\; \backslash frac\{\{2n\}\; \{(2n-1)\}\; \{(2n-2)\}\; \backslash ldots\; \{(2n-i+1)\}\}\{2\; \backslash cdot\; 3\; \backslash cdot\; 4\; \backslash cdot\; \backslash ldots\; \{(i+1)\}\}$$
Finally, she noted that $A\_\{n,1\}\; =\; \backslash frac\{2n\}\{n\}$ and used the following recurrence relation: $$A\_\{n,2(i+1)+1\}=A\_\{n,2i+1\}\; \backslash times\; \backslash frac\{2n-i\}\{2+i\}$$
Her famous program for $n=4$ is organized as follows:
Additionally, she described how to generalize this algorithm using two loops (called "cycles" in the article):
The following JavaScript program emulates Ada Lovelace's program:
Input: $n$ = ; Output ${B}_{\mathrm{}}\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:
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:
if
and
while
.
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.