/python/sum-of-multiples/sum_of_multiples.py

1
2
3
4
5
6
7
8
def sum_of_multiples(n, factors=[3, 5]):
    """Return the sum of multiples of the given factors up to and including n"""

    def has_factors(k):
        """Return true if k is divisible by any of the factors"""
        return any(k % i == 0 for i in factors if i)

    return sum(filter(has_factors, range(1, n)))

Comments

This problem is project euler 1.

In hackerrank, i work this by time complexity O(1). This is the link

Hackerrank

Just a joke, i am a php developer, i found pythoner like make code easier and easier...

XD

RryLee commented 22 March 2016 at 10:53 UTC

You're not logged in right now. Please login via GitHub to comment