in Programming: Project Euler, Problem 1 (Python 3.0)
Problem 1:
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.
This isn’t a problem that can be done very quickly by hand, so let’s just let the code speak for itself:
import timeit,time
start=time.time()
sum=0;
counter=0;
while counter<1000:
if counter%3==0 or counter%5==0:
sum=sum+counter
counter+=1
print(sum)
print(time.time()-star)
The time tools let me time how long it takes. When running this from the command prompt I get a time of .0019998550415 seconds, and it gets the right answer (which I’ll leave out in case you don’t want the spoiler).
Does anyone have a better way of getting code published on here?
Advertisements
Advertisements