Created
November 24, 2012 05:46
-
-
Save edalorzo/4138588 to your computer and use it in GitHub Desktop.
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
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