<jupyter_start><jupyter_text>"Problem 1: Multiples of 3 and 5"> Utilitize the basic properties of multiples and sums.- permalink: p1- toc: true- badges: true- comments: true- categories: [solution]  🔒 [Problem](https://projecteuler.net/problem=1)If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000. 🔐 Key Ideausing mathematical concepts to reduce the iteration number  🔑 Solution 🧭 Initial idea$O(1)$ complexity.Using the mathematical concept of sum of multiples exists as a pair in a range. For example, for the sum of multiples of 2 not exceeding 8, there would be 5 pairs with equal sum: (2, 8), (4, 6). These pairs sum to 2 + 8, which is the minimum and the maximum multiple. Numbers with odd number of multiples, we can just add `(min + max) // 2` to the sum.<jupyter_code># Target number used to limit iteration
k = 999

f = [3, 5, 15]  # factors
n = [k//3, k//5, k//15]  # number of multiples
max = [k - k%3, k - k%5, k - k%15]  # biggest multiples for each factor 
sum = []  # sum of each factors

for i in range(3):
    minmax = f[i] + max[i]
    s = (n[i] // 2) * minmax

    if n[i] % 2 is 1:
        s += minmax // 2

    sum.append(s)

print(str(sum[0] + sum[1] - sum[2]))<jupyter_output>233168<jupyter_text>> WARNING: Since it's defined in the problem as *'below 1000'*, it shouldn't contain 1000.> TIP: Consider the range with operands such as ``, etc.> NOTE: Clarify the *range* of the problem before diving into it.  🔨 Bruteforce method$O(n)$ complexity.<jupyter_code>s = 0
for i in range(1000):
    if i % 3 is 0 or i % 5 is 0:
        s += i

print(s)<jupyter_output>233168<jupyter_text>🔁 Function method (from overview)$O(1)$ complexity.> TIP: Use functons or external blocks for complicated for loops or loops with small iteration count.<jupyter_code>k = 999

def get_multiple_sum(f):
    n = k // f
    max = k - k % f
    sum = (f + max) * (n // 2)

    if n % 2 is 1:
        sum += (f + max) // 2

    return sum

print(str(get_multiple_sum(3) + get_multiple_sum(5) - get_multiple_sum(15)))<jupyter_output>233168
{'fraction_non_alphanumeric': 0.11572153065929, 'fraction_numerical': 0.04702627939142462, 'mean_word_length': 3.375, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '497309', 'license': 'Apache-2.0', 'n_tokens_mistral': 845, 'n_tokens_neox': 744, 'n_words': 318, 'path': '_notebooks/2021-06-10-p1.ipynb', 'repo_name': 'ntcho/project_euler_blog', 'size': '2169'}