Created
January 14, 2009 18:32
-
-
Save PEZ/46995 to your computer and use it in GitHub Desktop.
Python solutions to Project Euler Problem 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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