Wednesday, 2 October 2013

Python, Factors, and Primes

So my son had math homework on determining the factors of numbers. This was part of a larger mondule on factors, prime, and composite numbers. So after completing the homework together I asked him: "Do you think we can modify our times table program to calculate the factors of a number?".

He thought for a moment and then said: "Yes".

And so we set to it. As it turns out, the program required little change, but the significant new piece of learning was the moduls operator.

We essentially looped from 1 to the given number and took the modulus of the given number by the loop iteration. If the modulus had a value, then the loop iteration was not a factor. If there was no remainder, then it was a factor, which we would print.

Of course this also introduced to him the concept of the if test statement in python.