Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created November 24, 2012 05:46
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 edalorzo/4138588 to your computer and use it in GitHub Desktop.
Save edalorzo/4138588 to your computer and use it in GitHub Desktop.
Project Euler-Problem 1
def sumDivisible(m,n):
"""
Given a maximum integer m and a factor n, calculates the sum
of all divisors of n up to m.
"""
p = m // n
return n * (p * (p + 1)) // 2
def solve():
result = sumDivisible(999,3) + sumDivisible(999,5) - sumDivisible(999,15)
return "The sum of multiple of 3 and 5 up to 1000 is: %d" % result
def alternative(j, k, m):
"""
Naive alternative solution based on filtering a range.
"""
return sum(filter(lambda n: n % j == 0 or n % k == 0, range(1,m)))
if __name__ == '__main__':
print solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment