python course: add all multiples of 3 and 5

When you start learning programming, it’s common to begin with simple problems related to numbers. These problems help to understand the basics of algorithms and programming languages, and they are a great exercise to develop logical thinking. In this course, we will explore how to solve the problem of summing all multiples of 3 and 5 using Python.

Problem Definition

The problem we want to solve is as follows: Sum all natural numbers less than a given positive integer N that are multiples of 3 or 5.

For example, if N is 10, the multiples of 3 are 3, 6, and 9, and the multiple of 5 is 5. Therefore, the sum of all multiples of 3 and 5 is 3 + 5 + 6 + 9 = 23.

Basic Approach

A basic method to solve this problem is to use a loop to check all numbers from 1 to N-1 and check if each number is divisible by 3 or 5. If the number meets the condition, it is added to the total. The simplest form of code is as follows:


def sum_of_multiples(n):
    total = 0
    for i in range(n):
        if i % 3 == 0 or i % 5 == 0:
            total += i
    return total

The code above is very simple. The `sum_of_multiples` function takes an integer `n` as input and calculates the sum of numbers that are multiples of 3 or 5 among the numbers from 0 to `n-1`. This method performs adequately in most cases.

Explanation of Python Syntax

Now let’s take a closer look at the components of the Python code we used.

1. Function Definition

In Python, functions are defined using the `def` keyword. `sum_of_multiples(n):` is a function named `sum_of_multiples` that takes a parameter `n`. The function’s name should be intuitive so that it describes what the function does.

2. Variable Initialization

`total = 0` initializes a variable to store the sum we want to calculate. This variable will be used later to add the multiples of 3 and 5.

3. Loop

The `for i in range(n):` statement sets up a loop that iterates over the numbers from 0 to `n-1`. `range(n)` generates an object similar to a list, which returns a sequence equivalent to `[0, 1, …, n-1]`.

4. Conditional Statement

The `if i % 3 == 0 or i % 5 == 0:` statement checks if each number is a multiple of 3 or 5. The `%` operator returns the remainder, and if the number is divisible by 3 (remainder is 0), it is a multiple of 3. The same applies for multiples of 5. If this condition is true, the number `i` is added to the `total` variable.

Another Method Using List Comprehensions

Python provides various features to improve code readability and conciseness. One of these is list comprehensions. Using list comprehensions allows us to solve the above problem in a single line of code:


def sum_of_multiples_using_comprehension(n):
    return sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0)

This method combines the loop and conditional statements into one line, featuring the use of the `sum()` function to calculate the sum of the list. This form of code is intuitive and is useful when you want to maintain short code.

Considering Efficiency

The methods introduced above are intuitive and simple, but they may not be efficient for large values. As the number of iterations increases, the computational complexity can rise. Fortunately, we can use mathematical formulas to solve this problem more efficiently.

Mathematical Approach

Mathematically, we can use the formula for the sum of an arithmetic series to calculate the sum of multiples of 3 and 5. This method is especially useful when N is very large.

Multiples of 3: 3, 6, 9, …, the largest multiple of 3

Multiples of 5: 5, 10, 15, …, the largest multiple of 5

Common multiples should be excluded since they are counted multiple times.


def arithmetic_sum(n, r):
    count = (n - 1) // r
    return r * count * (count + 1) // 2

def efficient_sum_of_multiples(n):
    sum_3 = arithmetic_sum(n, 3)
    sum_5 = arithmetic_sum(n, 5)
    sum_15 = arithmetic_sum(n, 15)
    return sum_3 + sum_5 - sum_15

`efficient_sum_of_multiples` uses the `arithmetic_sum` function to calculate the sum of an arithmetic series. This function computes the sum of each multiple based on the formula `r x ((n-1)//r) x (((n-1)//r) + 1)/2`. The final result is obtained by adding the sum of the multiples of 3 and 5 and then subtracting the sum of the multiples of 15, which were added multiple times.

Conclusion

In this course, we explored various ways to calculate the sum of multiples of 3 and 5 using Python. We covered the basic iterative approach, a concise implementation using list comprehension, and a mathematically efficient method. By presenting diverse methods to solve this problem, we provided opportunities to enhance understanding of fundamental programming principles and mathematical thinking.

Experiencing solving problems in various ways helps improve programming skills. Additionally, it provides a chance to deepen understanding of algorithms and data structures.