How to use for and while loops to find unique multiples of a list of factors

Question:

Given a list of factors and a limit, add up all the unique multiples of the factors that are less than the limit. All inputs will be greater than or equal to zero.

Example
Suppose the limit is 20 and the list of factors is [3, 5]. We need to find the sum of all unique multiples of 3 and 5 that are less than 20.

Multiples of 3 less than 20: 3, 6, 9, 12, 15, 18
Multiples of 5 less than 20: 5, 10, 15

The unique multiples are: 3, 5, 6, 9, 10, 12, 15, 18

The sum of the unique multiples is: 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78

So, the answer is 78.

def sum_of_multiples(limit, multiples):

    n = 1
    sum_m = []

    for i in multiples:
        while i*n < limit: 
            sum_m.append(i*n)
            n+=1
            if i*n > limit:
                break
                
    return sum(sum_m)



The test results came back as "your tests timed out" suggesting the code is running slowly or there is an infinite loop or something similar so I am not able to see the error made.

Thinking it was an infinite loop issue, I modified the code to cater for break

def sum_of_multiples(limit, multiples):

    n = 1
    sum_m = []

    for i in multiples:
        while i*n < limit: 
            sum_m.append(i*n)
            n+=1
        else:
            break
                
    return sum(sum_m)

and tried another if statement

def sum_of_multiples(limit, multiples):

    n = 1
    sum_m = []

    for i in multiples:
        if i*n < limit: 
            sum_m.append(i*n)
            n+=1
                
    return sum(sum_m)

The if method was wrong as the answer was incorrect, but the else method that I used above gave me the same time out error. Wasnt sure what else to do.

Asked By: CLHE

||

Answers:

I believe that this solution will cover all your conditions:

def sum_of_multiples(limit, multiples):
  l1 = []
  for i in multiples:
    mul=1
    while i*mul<=limit:
      l1.append(i*mul)
      mul+=1
  sum_of_multiples = sum(list(set(l1)))
  return sum_of_multiples
Answered By: mis_swa
  def sum_of_numbers(limit,multiples):
      list_of_numbers=[]
      for i in multiples :
          for n in range(1,limit):
              if n*i>=limit:
                  break
              list_of_numbers.append(n*i)
       return sum(set(list_of_numbers))    

I think You can use this code its much easier and It’s simpler.

Answered By: 0nE

You could do:

def sum_of_multiples(limit, factors):
    multiples = set()

    for factor in factors:
        multiple = factor
        while multiple < limit:
            multiples.add(multiple)
            multiple += factor

    return sum(multiples)

(I renamed sum_m to multiples, i to factor and multiples to factors to make it a bit more readable, but they are still the same)
So:

print(sum_of_multiples(20, [3, 5]))

prints 78.

There are two problems with your program:

  1. n = 1 needs to be called in the for loop.
  2. Duplicates are counted separately.

To fix the second problem, I used a set (which can’t have duplicates, and therefore disregards them) instead of an array.

Answered By: sbottingota
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.