Skip to content

Instantly share code, notes, and snippets.

@PEZ
Created January 14, 2009 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEZ/46995 to your computer and use it in GitHub Desktop.
Save PEZ/46995 to your computer and use it in GitHub Desktop.
Python solutions to Project Euler Problem 1
"""Find the sum of all numbers below 1000 divisible by 3 or 5.
http://projecteuler.net/index.php?section=problems&id=1"""
#Brute force #1
sum(x for x in xrange(3,1000) if x % 3 == 0 or x % 5 == 0)
#Brute force #2
sum(set(range(3, 1000, 3) + range(5, 1000, 5)))
#Less brute (after reading the problem discussion PDF)
T = 1000
def sum_divisible_by(n):
p = T // n
return n * (p * (p + 1)) // 2
sum(sum_divisible_by(n) for n in (3, 5, -15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment