Project Euler Problem 1
So, I’m starting this much sooner than I would have anticipated but I suddenly got the urge to start.
Problem 1 is pretty straight forward:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
I love me some straight forward questions. Here is what I threw together:
total = 0
for x in range(1000):
if(x % 5 == 0 or x % 3 == 0):
total += x
print total
Originally I was thinking about skipping it, but thankfully I didn’t. There were a few things I learned from doing this problem:
-
Logical Operators in Python - instead of &&, || and !, they are and, or, and not as demonstrated on line 3
-
The range(n) function returns numbers from 0-(n-1), not 1-n. I don’t know why I thought the latter. I guess I am already in vacation mode.
-
It is hard to remember to put colons before the start of an indented block. I also continuously want to use curly braces and semicolons.
I don’t know if I’m done working on this yet. There are a lot of ways to answer this problem. The few answers I read on the Project Euler Problem 1 forum were interesting but I stopped reading in case I do decide on working on this some more. I don’t want to be influenced.