Donkeys, Logs, Snakes, Oh My!

01_tunapasta
For the last several years in the United States and the UK, there has been a strong push to teach primary and secondary-school students how to code. Whether this is a good idea or not, I personally have enjoyed learning about programming. One of my current hopes is to do more programming using the Python programming language.

python-logo-master-v3-tm-flattenedNow, I won’t get too much into coding—my intention for this blog is to focus on math—but I just can’t resist to somehow meld these personal and professional interests. Namely, how can I use my basic Python programming skills to explore math concepts and solve math problems?

As a starter, I thought that I would introduce readers to the Python interpreter. Basically, this is a program that accepts commands written in Python and executes them immediately. If you’re using Mac OS X or some other Linux\Unix operating system, then the chances are very strong you can just open “Terminal” or your favourite shell and type “python” to get the interpreter running. If you’re on Windows or just have no clue what I meant in the last sentence, then I recommend downloading Python and using IDLE, which you can click on to launch like any other app. I’ll be using IDLE to talk about Python in this entry and probably future ones too.

Suppose you are asked to solve the following:

Currently, there are six donkeys on Nick’s farm. The donkey population grows at a rate of 2.5 times per year. When will Nick have 18 donkeys on his farm? Round your answer to the nearest tenth of a year.*

Typically, we would solve the problem by first modelling the situation as an exponential equation and then rewrite the equation in its equivalent logarithmic form:

00_longsolution

Nothing wrong with this solution except that, in the third-to-last line, we have to divide the logs of 3 and 2.5 by each other because most scientific calculators won’t let you use anything but base 10 or base e (in the case of ln).

Now let’s see how we could solve this problem using the Python interpreter. Open IDLE and hit Enter once.  First, we’ll want to bring into the interpreter environment all the useful math functions we’ll need, in our case, log. These functions exist in the math module :

02_importmath

Next, we’ll write the following to calculate our answer. Notice how we refer to the math module just before we write our log function. The second number in brackets refers to our base, thus avoiding the need to do the log division of  3 and 2.5 as described above.

03a_logcalc_calc

Okay, great, but where’s our answer? Well, it was actually stored in the variable x. To see it , just type x and hit Enter.

03b_logcalc_ans

To round the answer to the nearest tenth, write the following. The ‘1’ implies we want the answer to one decimal place.§

04_round

Finally, this was a word problem, so let’s do what we were told in school and express our final answer as a sentence.

05_answer

That’s a lot of hay. Good luck, Nick😆!

grey-donkey_cropped_2

*I have no idea if this situation makes any sense with regard to population growth for donkeys in the real-world, or even a priori -wise.
† I suppose that it would be possible to change the base on a fancy graphic calculator, but as I don’t own one, I can’t be sure.
‡ Words like “store” and “variable” are helpful to understand what the code is doing, but may not actually mean what they imply vis-a-vis how Python actually works. What Python is actually doing is beyond the scope of this entry and, probably, beyond what I currently know about Python too.
§More technical Python stuff. This line actually converts our answer from a real number into text, hence the quotes around the answer. I won’t explain any further except to say that if you type 1 + 2 as numbers your get 3, but if you type ‘1’ + ‘2’ as text you get ’12’.

One thought on “Donkeys, Logs, Snakes, Oh My!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s