Skip to main content
added 67 characters in body
Source Link
Leonid
  • 901
  • 7
  • 10

I would keep it simple. In this particular case I would do:

def mySummy_sum(start, end, *divisors):
    return sum(i for i in rangexrange(start, end + 1) if any(i % d == 0 for d in divisors))

if __name__ == '__main__':
    print(mySummy_sum(0, 999, 3, 5))

Because it is readable enough. Should you need to implement more, then add more functions.

There is also an O(1) version(if the number of divisors is assumed constant), of course.

Note: In Python 3 there is no xrnage as range is lazy.

I would keep it simple. In this particular case I would do:

def mySum(start, end, *divisors):
    return sum(i for i in range(start, end + 1) if any(i % d == 0 for d in divisors))

if __name__ == '__main__':
    print(mySum(0, 999, 3, 5))

Because it is readable enough. Should you need to implement more, then add more functions.

There is also an O(1) version(if the number of divisors is assumed constant), of course.

I would keep it simple. In this particular case I would do:

def my_sum(start, end, *divisors):
    return sum(i for i in xrange(start, end + 1) if any(i % d == 0 for d in divisors))

if __name__ == '__main__':
    print(my_sum(0, 999, 3, 5))

Because it is readable enough. Should you need to implement more, then add more functions.

There is also an O(1) version(if the number of divisors is assumed constant), of course.

Note: In Python 3 there is no xrnage as range is lazy.

Source Link
Leonid
  • 901
  • 7
  • 10

I would keep it simple. In this particular case I would do:

def mySum(start, end, *divisors):
    return sum(i for i in range(start, end + 1) if any(i % d == 0 for d in divisors))

if __name__ == '__main__':
    print(mySum(0, 999, 3, 5))

Because it is readable enough. Should you need to implement more, then add more functions.

There is also an O(1) version(if the number of divisors is assumed constant), of course.