This Python script mimics Babbage's Difference Engine

Python once again takes on Charles Babbage's Difference Engine.
56 readers like this.
Math formulas in green writing

João Trindade. Modified by Jason Baker. CC BY-SA 2.0.

In Use this Python script to simulate Babbage's Difference Engine, Python offered an alternative solution to Babbage's problem of determining the number of marbles in a two-dimensional pyramid. Babbage's Difference Engine solved this using a table showing the number of marble rows and the total number of marbles.

After some contemplation, Charles Babbage's ghost replied, "This is all well and good, but here you only take the number of rows and give the number of marbles. With my table, I can also tell you how large a pyramid you might construct given a certain number of marbles; simply look it up in the table."

Python had to agree that this was indeed the case, yet it knew that surely this must be solvable as well. With little delay, Python came back with another short script. The solution involves thinking through the math in reverse.

MarbNum = (N * (N + 1))/2

Which I can begin to solve with:

N * (N + 1) = MarbNum * 2

From which an approximate solution might be:

N = int(sqrt(MarbNum * 2))

But the integer N that solves this might be too large by one, so I need to test for this. In other words, the correct number of rows will either be N or N-1. Here is the final script:

#!/usr/bin/env python
# babbage2.py
"""
Using Charles Babbage's conception of a marble-counting operation for a regular 
pyramid of marbles, starting with one at the top with each successive row having 
one more marble than the row above it. 
Will give you the total number of rows possible for a pyramid, given a total number 
of marbles available. 
As a bonus, you also learn how many are left over.
"""
import math

MarbNum = input("Enter the number of marbles you have:  ")
MarbNum = int(MarbNum)

firstguess = int(math.sqrt(MarbNum*2))

if (firstguess * (firstguess + 1) > MarbNum*2):
    correctNum = firstguess - 1
else:
    correctNum = firstguess

MarbRem = int(MarbNum - (correctNum * (correctNum + 1)/2))
# some grammatical fixes
if MarbRem == 0:
    MarbRem = "no"
 
if MarbRem == 1:
    marbleword = "marble"
else:
    marbleword = "marbles"
    
print ("You can have",correctNum, "rows, with",MarbRem, marbleword, "remaining.")

The output will look something like this:

Enter the number of marbles you have:  374865 
You can have 865 rows, with 320 marbles remaining.

And Mr. Babbage's ghost was impressed. "Ah, your Python Engine is impressive indeed! Surely it might rival my Analytical Engine, had I had the time to complete that project."

What to read next
Tags
Greg Pittman
Greg is a retired neurologist in Louisville, Kentucky, with a long-standing interest in computers and programming, beginning with Fortran IV in the 1960s. When Linux and open source software came along, it kindled a commitment to learning more, and eventually contributing. He is a member of the Scribus Team.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.