Hack your homework: grid multiplication

Hacking your homework is not cheating. To write some code that does what you want, you’ll have to think about the maths (or whatever) very carefully. They say the best way to learn something is to teach it. When you write a program, you are teaching your computer to do something.

This program teaches the computer to use the grid method of multiplication. The key to a good maths homework hack is to get the computer to output the “working out” as well as the answer. Here’s a little screenshot of my program’s output.

As you can see, this program takes two numbers and prints out a little table representing the “grid method” way of doing multiplication and then gives you the answer.

Here’s the first part of the code:

The program starts with the usual comment lines, explaining what we are doing. Then we need to import the “math” module, because we are going to need a new mathematical function: math.pow().

I then define a function. We’ve looked at functions a bit before – by doing it this way, I can separate my numbers into hundreds, tens and units just by calling: split().

I create an empty list: grid, into which I can put the values I create. I also initialize a couple of variables, j  and k. The loop that follows is actually the most fun part of the program. I was wondering how to separate out the hundred, tens, units and so on. This is how I did it. If you can think of a simpler way, please let me know.

The loop basically takes the number and divides it by increasing powers of 10 (ie 10, 100, 1000 etc.). This is why we needed the maths module. To raise a number to a power, we need to use math.pow(). For instance if we want to square 3, we would call math.pow(3,2). Here we are using powers of 10. 10 to the power 1 is 10, squared it’s 100 and so on.

The modulus of each of these divisions gets put in “y” to start with. For instance if your number is 14, the remainder when you divide by 10 is 4 – in other words the unit part of the number. If the result is not zero, we add it to the list (we don’t want to bother with the zeros – if you multiply by 1024, for instance, you can ignore the zero for the purposes of your grid). Next I take the y away from x, because I don’t need to worry about that number any more – it’s safely stored in my list. We add one to k, so that next time through the loop we will be using the next power of 10. Finally we divide x by that next power – if the answer is zero, there’s nothing else to do and the while loop will stop.

At the end of the loop I sort the numbers in descending order and send the list “grid” back to the main part of the program.

Here’s the next part:

That’s the hardest bit done. The main part of the program gets the two numbers, sends them off to split() and stores the lists that are produced.

The next bit prints the top line of our table. I’ve made the column-width 7 – you’ll need to alter this if you are working with bigger numbers.  I use a “for” loop to print all the numbers in the first list. I have used “repr” here. This is a bit like “str” – it converts a number into a human-readable form. It also takes the method “.rjust(x)” which right-justifies the value in a column of width “x”.

The next bit is simple too – there’s a nested loop which goes through all the numbers in both lists, multiplies them and prints them. I used “col” to collect the sum of these numbers for the “totals” column and “ans” to keep a running total of that column.

The final line prints the result.

At the moment this program only deals with integers. If you have to use the grid multiplication method for floating point numbers, you could modify this program to deal with that. (Hint: 10 to the power -1 = 0.1).

happy coding!



Tags: ,

7 Responses to “Hack your homework: grid multiplication”

  1. francescodifusco Says:

    Reblogged this on FRANCESCO DI FUSCO.

  2. antiloquax Says:

    Thanks Francesco!

  3. francescodifusco Says:

    you are wellcome

  4. Hack your homework: grid multiplication « Team Python | FRANCESCO DI FUSCO Says:

    […] Hack your homework: grid multiplication « Team Python. […]

  5. . Target bigger markets. Says:

    An outstanding share! I’ve just forwarded this onto a colleague who was doing a little homework on this. And he actually ordered me dinner due to the fact that I stumbled upon it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending the time to discuss this issue here on your blog.

  6. antiloquax Says:

    Thanks for your comment. It’s great to hear when someone has found something useful.

  7. pirater un compte facebook en ligne Says:

    Your way of explaining everything in this paragraph is really pleasant, every one can simply be aware of it, Thanks a lot.

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 )


Connecting to %s

%d bloggers like this: