Lunar Arithmetic
January 22, 2019
Over at Numberphile, Neil Sloane (yes, that Neil Sloane), talks about lunar arithmetic, an “other-worldly” way of doing simple math:
Lunar addition and multiplication work digit by digit, the same as terrestrial addition and multiplication, except that the plus and times tables are unusual. Addition is done by taking the larger of the two digits, so 1 + 3 = 3 and 7 + 4 = 7. Multiplication is done by taking the smaller of the two digits, so 1 * 3 = 1 and 7 * 4 = 4. Thus:
3 5 7 3 5 7 + 6 4 * 6 4 ------- ------- 3 6 7 3 4 4 3 5 6 ------- 3 5 6 4There are no carries. There is no subtraction or division, since the results would not be unique.
Your task is to write programs that perform lunar addition and multiplication. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
57s/0/1/ ; sorry.
In Python.
Here’s a solution in C.
Example Usage:
Here’s another Python solution, I nicked a few bits of Paul’s solution, including use of zip_longest, which I didn’t know about. The Lunar class internally uses the list of digits representation. It’s fun to implement the Sieve of Eratosthenes too:
A Haskell version.
And apparently, there are applications: https://twitter.com/numberphile/status/1348613413619040256