""" Find the sum of all multiples of 3 or 5 below 1000 Jim M | in class Oct 6 | GPL """ ### This whole file is really just this ... ### but with a clearer division into functions and tests. #total = 0 #for i in range(n): # if i % 5 == 0 or i % 3 == 0 # total = total + i #print total def multiple_of(i, n): """Return True if i is a multiple of n >>> multiple_of(10, 3) False >>> multiple_of(12, 3) True """ return i % n == 0 def sum_multiples(n): """Return sum of numbers that are a multiple of 3 or 5 up to n >>> sum_multiples(10) # 3 + 5 + 6 + 9 = 23 23 """ total = 0 for i in range(n): if multiple_of(i, 5) or multiple_of(i, 3): total = total + i return total def main(): print sum_multiples(1000) if __name__ == "__main__": import doctest doctest.testmod() main()