I’ve just stumbled upon Project Euler. It’s a nice little site with mathematical puzzles to be solved using a computer. For example, this first problem is to find the sum of all the natural numbers who are multiples of either 3 or 5, and are below 1000.
There are hundreds of such puzzles, most of them more challenging, so I’ll allow myself to post the answer to this first problem:
#!/usr/local/bin/python sum = 0 for number in range(1, 1000): if number % 3 == 0 or number % 5 == 0: sum += number print sum
And what is great is that once you solve a problem, you get access to a pdf file with an overview of the problem and efficient solutions. The above solution I wrote, which is quite naive, can be computed in O(1), had I first used a pencil and paper as my IDE, and not Vi. Can you see how?