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?

## No comments:

## Post a Comment