How Python Can Help

Sorry it’s been awhile. I love being a dad and giving an attempt at that while going to graduate school is not an easy combination. But I’m still a heavy consumer of all the great Mathematics posts I see on Twitter. Usually when I see a post that catches my eye, I’m able to contemplate how I respond… but then immediately get sucked into preparing food or trying to set up a new gadget or just making faces at the kiddo.

But this one really got into my brain. I thought: what the hell kind of mutant number always contains the digit 1? I’m pretty good at simply multiplying numbers in my head, so I thought I could solve this fairly quickly.

First I tried some simple ones. 11? Nah, we can see that it has no 1 when multiplied by… like anything. 21… 31… 41… all lose their ‘oneness’ when multiplied by anything. Time to think bigger…

How about 379? Dude that doesn’t even have a 1 right now.

Ok, let’s get crazy. How about the number 123,456,789? Surely when you multiply it by anything a magical one pops into view… Gotta bust out a calculator for that one. Ok, type it in, multiply by 2 and I get 246,913,578. Aha, a 1! Clearly I’ve solved this problem, let’s just check once more to prove to myself how AWESOME I am at solving math puzzles. So we type in 123,456,789, multiply by 3 and… 370,370,367… No 1 in sight.

Clearly this is my cup of tea: simple-on-the-face, but can’t be solved without effort. What I want to show here is another way to think about problems like this. How can computers, and more specifically Python can help solve this.

I’ve been studying programming in the business sense for the last semester (see the zero posts I’ve made in last 6 months). But I want to show you how easy it is to get started with a language like Python – a free, open source programming language that is a breeze to install and use. Python has tutorials all over the place if you want to try on your own, but here’s a starter package.

First, we need a function to test whether a number actually has a 1 in it or not. That might seem unnecessary at first blush, after all can’t we just look at the number? Well… I guess it’s possible, but then it can’t be automated. The purpose of setting up programming is to let the computer do all the heavy lifting.

This program can be pretty simple, we can even get it a badass name like “The MF’ing One Extractor”, or maybe “one_tester” for short. Now Python functions are just like algebraic functions – you pass some stuff in and get something out. In this case, we’re going to pass a number in and get either True or False (stored as ‘flag’) out based on whether it contains a 1 (and yes the capitalization matters, Python is a little finicky about punctuation). Here’s our script:

def one_tester(word):
if ‘1’ in str(word):
flag = True
else:
flag = False
return flag

Now the main thing I’ve learned is that when we test programs like this, we need to try some weird inputs to see if our script holds up. Let’s try a few inputs: 435909; 000000001111100000000, and just for kicks, let’s throw in ‘brandon1rules’.

Capture

Perfect, we could keep testing, but seems like that part is working. Now that we have a function for testing for ones, we can do something a little more advanced. For a given candidate number to the solution of the puzzle, here is what we would like to do:

  • Test if the candidate contains a ‘1’
  • If it does not, stop the script and send a message to the user
  • If it does, try multiplying by 2
  • Go back to the first step and continue the process

Essentially we’re trying to do the opposite of the puzzle: find a multiple of the user’s chosen number that doesn’t have a ‘1’.

Script:

def number_tester(num):
flag = False
multiplier = 1
while (flag == False):
if (one_tester(multiplier * num) == True):
multiplier = multiplier + 1
else:
print str(num) + ” does not have a 1 when multiplied by ” + str(multiplier)
flag = True

Now, is this script the best we can do? Absolutely not. But it really gives the user the power to do something really simple and easy, rather than manually trying to find a multiple of a candidate that doesn’t contain a 1. In fact this program is so poor that if we actually found a candidate whose multiples always contain a 1, it would be caught in an infinite loop. But this is the kind of beginning that takes the burden of getting started off of the puzzle solver. I can start plugging in numbers quickly and try to find patterns.

Say you have a hunch that the number 574,381 is the one to solve this problem. Instead of trying to multiply over by one and manually checking for a ‘1’, you can simply execute the script:

Capture

Now we’ve been freed up from manually checking numbers and can move our brainpower to looking for patterns of numbers.

Over the last couple of years, I’ve really been converted. I think that we have to prioritize computer literacy, not how to format generic powerpoints about solar systems, but rather being able to have computers take the burden of certain tasks . We have super computers in our pockets and on our laps, and they can be used so much more powerfully than glorified word processors.