0

Sum Of Multiples Program

This program was made for the Project Euler problem "Multiples of 3 and 5". Although this does solve the problem, I want to further improve on the code. Specifically, make it so that you may input an unlimited number of items, instead of being limited to two base numbers.

Somehow, I figured to use a list, make the function recursive, or use a function within the function.

def SumOfMultipleBase2(multiple1 , multiple2 , start , limit):
sum = 0
    for i in range(start , limit):
        if 1%multiple1 == 0 or 1%multiple2==0:
           sum += 1
print('The sum of the multiples of ' , multiple1, ' or' , multiple2 , 'between ', start , ' to ' , limit , ' is ' , sum , '.')
GKE
  • 960
  • 8
  • 21

1 Answers1

1

Unless you want to use '*args' to pass your numbers I think a list or tuple will be okay. The code then could look something like:

def som(start, stop, base): ## base is a list of the numbers you want to use as possible base.
  n = 0
  for i in range(start, stop):
    for b in base:
      if i % b == 0: ## if any match is encountered add 1 and break out of the loop.
        n += 1
        break
  print("there are", n, "multiples of any number in", base, " between", start, "and", stop, ".")
Chris
  • 710
  • 7
  • 15