Even faster, since it doesn't rely on an iterator. Especially big numbers yield noticeable differences in execution time.
def euler(num): sum_range = lambda x: x*(x+1)//2 return 3*sum_range((num-1)//3) + 5*sum_range((num-1)//5) - 15*sum_range((num-1)//15)
- def euler(num):
return sum(range(3,num,3)) + sum(range(5,num,5)) - sum(range(15,num,15))- sum_range = lambda x: x*(x+1)//2
- return 3*sum_range((num-1)//3) + 5*sum_range((num-1)//5) - 15*sum_range((num-1)//15)
test.assert_equals(euler(10), 23) test.assert_equals(euler(1000), 233168) test.assert_equals(euler(100000), 2333316668) test.assert_equals(euler(10000000), 23333331666668)
- test.assert_equals(euler(10), 23)
test.assert_equals(euler(1000), 233168)- test.assert_equals(euler(1000), 233168)
- test.assert_equals(euler(100000), 2333316668)
- test.assert_equals(euler(10000000), 23333331666668)