Skip to content

Instantly share code, notes, and snippets.

@ericdorsey
Created September 1, 2019 19:50
Show Gist options
  • Save ericdorsey/dfc3ce909128a307c8697bcbcdd3c03e to your computer and use it in GitHub Desktop.
Save ericdorsey/dfc3ce909128a307c8697bcbcdd3c03e to your computer and use it in GitHub Desktop.
Rhind Papyrus: Multiplication by Doubling and Halving (Mediation & Duplation)
#!/usr/bin/python3
# Multiplication by Doubling and Halving (Mediation & Duplation)
# http://www.mathnstuff.com/math/spoken/here/2class/60/egyptm.htm
def rhind_papyrus(num1, num2):
# Egyptian Reed Papyrus Multiplication
num1_floors = []
num2_doubles = []
# Add the initial values to each array
num1_floors.append(num1)
num2_doubles.append(num2)
while num1 > 1:
num1 //= 2
num1_floors.append(num1)
for i in range(len(num1_floors) - 1):
num2 *= 2
num2_doubles.append(num2)
#print(num1_floors, num2_doubles)
zipped = list(zip(num1_floors, num2_doubles))
#print(zipped)
for i in zipped:
print(i)
running_total = 0
for i in zipped:
if (i[0] % 2) != 0:
print(f"Left column floor division result not even; will add: {i[1]}")
running_total += i[1]
print(f"Total: {running_total}")
rhind_papyrus(10, 14)
print()
rhind_papyrus(41, 59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment