Solving the sum of multiples in python

The sum of multiples is a number theory question that takes two natural numbers and then asks you to provide the sum of both numbers up until a range. A natural number is any whole positive number, such as 1,10, or 192. To understand what this problem requires let’s get an understanding of some sample input and output. If given the two natural numbers 5 and 10, with the upper bound of 40, then here’s how the multiples of each number will look:

Multiples of 5: 
5, 10, 15, 20, 25, 30, 35
Multiples of 10: 
10, 20, 30

Therefore, the sum of multiples of 5 and 10 will be 5 + 10 + 10 + 15 + 20 + 20 + 25 + 30 + 30 + 35 which equals 200.

Below is one solution to solve the problem. Below is the breakdown of logic to solve the problem:

  1. We’ll created two separate lists for the multiples of each number. The lists will be named list_multi1 and list_multi2.
  2. We will combine the two lists.
  3. We will print the summation of the merged lists.
  4. We will also print other data such as the elements in each list and the elements in the combined list.
  5. Since we’re unsure of all of the multiples of two numbers, we can wrap the core logic in a while loop, and exit when the sum of multiples of both lists has reached their upper bound.

Here’s the solution coded in python:

def sum_multiplies(multi_1, multi_2, n):
    """ computes the sum of multiples for any two integers
    The factors of an integer multi_1 is added to a list
    named 'list_multi1.' The factors of an integer multi_2
    is added to a list 'list_multi2.' The combined list of
    merged is sorted. 
     """
    list_multi1, list_multi2, counter = [], [], 1
    merged, sum = 0, 0
    while True:
        a = multi_1 * counter
        b = multi_2 * counter
        if a >= n:
            pass
        else:
            list_multi1.append(a)
        if b >= n:
            pass
        else:
            list_multi2.append(b)
        counter += 1
        if a > n and b > n:
            break
        a = multi_1
        b = multi_2
    merged = sorted(list_multi1 + list_multi2)
    for y in merged:
        sum += y
    print('The sum of multiples of {} and {}: {}'.
          format(multi_1, multi_2, sum))
    print('The multiples of {}: {}'.format(multi_1, list_multi1))
    print('The multiples of {}: {}'.format(multi_2, list_multi2))
    print('The multiples for {} and {}: {}'.format(multi_1, multi_2, merged))

Here’s some tests for the function sum_multiplies:

>>> sum_multiplies(5, 10, 45)
The sum of multiples of 5 and 10: 280
The multiples of 5: [5, 10, 15, 20, 25, 30, 35, 40]
The multiples of 10: [10, 20, 30, 40]
The multiples for 5 and 10: [5, 10, 10, 15, 20, 20, 25, 30, 30, 35, 40, 40]
>>> sum_multiplies(5, 7, 100)
The sum of multiples of 5 and 7: 1685
The multiples of 5: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
The multiples of 7: [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
The multiples for 5 and 7: [5, 7, 10, 14, 15, 20, 21, 25, 28, 30, 35, 35, 40, 42, 45, 49, 50, 55, 56, 60, 63, 65, 70, 70, 75, 77, 80, 84, 85, 90, 91, 95, 98]
============================================================================ Want to learn how to use Python's most popular IDE Pycharm? In the free pdf guide "Getting the Hang of PyCharm" you'll learn all of the amazing features in PyCharm along with how to get started with data science. Subscribe to the Purcell Consult newsletter and get started A.S.A.P.