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!

antiloquax

Tags: hack your homework, Python

10/03/2012 at 7:23 am

Reblogged this on FRANCESCO DI FUSCO.

10/03/2012 at 7:41 am

Thanks Francesco!

11/03/2012 at 12:01 pm

you are wellcome

01/12/2012 at 5:44 pm

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

26/03/2013 at 10:08 am

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.

26/03/2013 at 10:34 am

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

28/09/2013 at 7:46 pm

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