Wednesday, August 20, 2014

Counting, adding, multiplying, and powers are just repeats of each other

This past winter, I somehow was led to an interesting insight that the basic functions in math are simply repetitions of each other.  Addition is just repeated counting, multiplication is simply repeated addition, and powers are just repeated multiplication.  The mental picture breaks down with weird stuff like negatives and fractions, but it can still form a nice intuition basis for WHY we wanted to invent these functions as an earth family and why they continue to be useful to us today.

Since the concept is a little abstract, and I wasn't sure if I fully believed it myself, I wrote a simple program in Python that has four functions that call each other.  For those of you new to reading Python code, here are the key things to know:

• Every function starts with a name and inputs.  To declare that it is a function, "def" is used to start the line.  Then the function name follows.  Finally, in the parentheses, are the input(s) passed into the function.
• At the end of a function that computes something and wants to send it back, you need to create a "return" statement.  This simply sends back the answer to the place that called it.
• On each line with an equal sign, the statement to the right is executed first.  Then it is saved in the variable on the left of the equal sign for later use.
• The statement "for i in range(__)" is a Python way of saying "loop __ times".  The things that are looped are indented one extra tab from the "for..." line.
• Finally, the "#" sign is not a hashtag!  It starts a comment on that line.  This means that everything after the "#" is NOT code, but an explanation of what is going on for the reader.  When writing the code on my computer, the text editor changes its color (as I have also done here) to help you notice that difference.

def count(initial):  # the count function takes an initial value
return initial + 1  # it adds one and return the result

def add(val_1, val_2):  # the add function takes any two positive integers
answer = val_1  # start counting from the first value
for i in range(val_2):  # the number of times to count forward in the loop
answer = count(answer)  # count once on the old answer
return answer  # all done!

def multiply(val_1, val_2):  # the multiply function takes any two positive integers
answer = 0  # start the repeating adding from 0
for i in range(val_2):  # the number of times to add in the loop
return answer  # all done!

def power(base, exponent):  # the power function take base and exponent
answer = 1  # start the repeat multiplying from 1
for i in range(exponent):  # the number of times to multiply in the loop
answer = multiply(answer, base)  # multiply once on the old answer
return answer  # all done!

When I call "add(7,5)", it returns "12".  This required the program to call "count" 5 times.  When I call "multiply(3,6)", it returns "18".  This required the program to call "add" 6 times, which in turn resulted in 18 calls to "count".  Finally, when I call "power(4,3)", I get "64".  This required 3 calls to "multiply", which made 12 calls to "add", which made 84 calls to "count".  The point is that the higher order functions all rely on the ones below them and just add one more layer of repetition.

If you're reading this, I hope this sparks some new ideas for you!  I would also love to hear if you have ideas on extending this.  It would be so cool to fully build up algebra in a computational world!

1. 1. 