Benny's Adventures

in Programming: Project Euler, Problem 1 (Python 3.0)

http://projecteuler.net

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?

28 June 2010 - Posted by | Programming | , , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: