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:
where the coefficients
's are the
Bernouilli numbers, starting at
.
For any integer ,
she considered the following recurrence relation:
where
and for each integer
Finally, she noted that
and used the following recurrence relation:
JavaScript Emulation (private class fields/methods demo)
The following JavaScript program emulates Ada Lovelace's program:
Input:
= ; Output
.
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.