Last modified on 12 October 2013, at 19:52

Python and Math

Welcome to Python and Math!Edit

Python and Math is an introductory approach to learning how to apply programming skills to solving math problems, while presenting high-level content with low-level terminology. Both experienced and beginner programmers will benefit from what this Wiki-book has to offer.

Here are the main objectives:

(1) Introduce programmers to solving math problems using programming.

(2) Practice problem-solving skills using programming.

(3) Introduce new skills and techniques that increase the efficiency of programs.

So as a starter, here is an example of a problem you would solve using programming:

Find the 10001th prime number, given that 2 is the first and 3 is the second.

Programming + MathEdit

The main benefit of using programming in problem-solving is where the limitations of pencil/paper become apparent: time. Solving problems by hand can take a long time, especially solving a problem as the one above. Because there are no patterns in prime numbers (as we know), in order to get the 10001th prime, we must write them all out, in hand, one by one. Won't that take a long time! Essentially computers are just fast-number crunchers - we use them to calculate numbers really fast, but only combined with the programmers mathematical genius can really hard problems be solved.

If you are well-versed in programming, then you will find skills picked up in this book easy to learn; math and programming come hand-in-hand. You may skip to the second step.

If you are unfamiliar with how to program, don't be afraid! I wont necessarily teach you myself, but through personal experience, i will supply you with internet sources that are the most helpful for a young programmer. I suggest learning Python as it is easy to pick up, user-friendly, and simple. Although it is not as powerful as C++ for example, it still gets the job done. In the end, it is the brain of the programmer than contributes to the best (and fastest) solution.

So first of all, download the latest 2.x.x version of Python from and install it. Then, read this entire Wiki-book on the basics of programming: If you want, you will also find this site helpful for other syntax tricks: (Read Sections 1-5, or more if you wish).


As for practicing solving problems with programming skills, this Wiki-book will use and abuse the ProjectEuler site: Essentially, this site has hundreds of different problems that can only (99% of the time) be solved using programming. They increase in difficulty, as they start relying more on statistical maths and programming tricks.


This Wiki-book will not give the answers to these problems out; such action will only contradict your skill-development. Instead, tips and strategies will be offered here and there that help in general problem solving ability, but not explicitly give away the method of obtaining the answer. Remember: solving a problem with your own strategy is 99% of the fun (the other 1% being the bragging-rights).

Dynamic ProgrammingEdit

The main bulk of what this Wiki-book will discuss will be on how to improve your programs so that they run faster and more efficiently. A more formal introduction to this will be made later, but below is an example of what I mean.

A function evaluating if input integer is prime (in Python script):

def prime(input):
    for n in range(2, input):
        if input%n == 0:
            return False
    return True

Essentially, this evaluates whether integer x can be divisible by any number less than it (n = [2, 3, 4, ... , x-2, x-1]). However, there are two redundancies in this method:

1. If input is not divisible by 2, it will not be divisible to any other even number (4, 6, 8, ...).

2. It is not needed to evaluate integers above the square root of the input number. To elaborate, here is an example:

Evaluate if 127 is prime:
127 divisible by 2? No.
127 divisible by 3? No.
127 divisible by 5? No.
127 divisible by 7? No.
127 divisible by 9? No.
127 divisible by 11? No.
Therefore 127 is prime.

We do not need to evaluate if 127 is divisible by 13, because 1271/2 is around 11.27, which is smaller than 13. If 127 was divisible by 13, then 127/13 would be less than 13 itself and therefore must give an integer already tested. As another example, 143 will be checked if it is prime. Here it will be shown that 13 is not needed to be evaluated once again (1431/2 ≈ 11.96).

143 divisible by 3? No.
143 divisible by 5? No.
143 divisible by 7? No.
143 divisible by 9? No.
143 divisible by 11? Yes → 143/11 = 13
(143 divisible by 13 not evaluated)
Therefore, 143 is not prime

Hence, our original prime function can be improved using these two suggestions, and now runs at a greater speed! For example, when evaluating if 10001 is prime, the function went through 10000 different numbers. But with these improvements, it only has to go through 50!

from math import ceil
def prime(input):
    for n in [2] + range(3, int(ceil(input**0.5)), 2):
        if input%n == 0:
            return False
    return True

Now, this only tests odd numbers (≥3) and ≤ the square root of the input (hence the ceil function, which can be replaced with a simple "+1").

So here is the lesson to learn from this: the gun is only as accurate as you are.


So, start off by creating a ProjectEuler account, then sort the problems list in ascending difficulty. Starting with the easy ones first will help you go through a lot of troubles in the beginning. So here is your assignment for the next chapter:

- Finish Problems 1 and 2 (without cheating!!!)