Modular Arithmetic & Project Euler Problem One
Tags: projecteuler, python, algorithms, beginners
Published: 2020-08-07 03:11:13

---
title: Modular Arithmetic & Project Euler Problem One
published: true
description: For the #projecteuler100, I'm taking a look at problem one, because you've gotta start somewhere.
tags: projecteuler, python, algorithms, beginner
---

In January 2020, which was approximately three years ago by my count, I happened to see this post:
{% twitter 1213168751413207042 %}

I vaguely thought this sounded like a cool idea and that I should do it and write blog posts about it. I've chipped away at [Project Euler](https://projecteuler.net/) before, and as of writing have solved about half of the first 100, although some through methods that I would be embarrassed to tout in a programming blog post.

A lot has happened since January, but I'm going to try Lindsey's 100 days of blogging in combination with this challenge:
{% twitter 1291129487116050432 %}

I want to tell you straight up, right now, that we are absolutely not getting through the first 100 Project Euler problems in the next 100 days, but I want to start chipping away a bit each day and hopefully finish them all eventually. I am not planning to publish every day but stick with me.

Now, I also want to say by way of preface: these are puzzles that are fun to try yourself! There will be spoilers here, read at your own risk!

Also, Project Euler is pretty math-y. If that's not your thing, it's cool! There's plenty of programming that doesn't get this deep into math. And I'll try to bring in relevant programming and computer science concepts too when I can, because I'm not all about math either tbh

Okay, without further ado, [problem one](https://projecteuler.net/problem=1).

> Find the sum of all the multiples of 3 or 5 below 1000.

This sounds similar to the classic [FizzBuzz interview question](https://www.byte-by-byte.com/fizzbuzz/) and can be solved similarly.

The key idea here is that we need a way to quickly check whether a number is divisible by another number. We could do something like dividing the numbers and checking for figures after the decimal, but that seems arduous and might be annoying to ensure that the numbers are treated as decimals rather than automatically-rounded integers.

The magic here is the [modulus operator](https://en.wikipedia.org/wiki/Modulo_operation). It may not have been covered in your math classes unless they covered discrete math or computer science, but the idea is pretty straightforward, it returns the _remainder when the first number is divided by the second_.

Here are a few examples:
``` py
10 % 5 == 0
23 % 5 == 3
4 % 5 == 4
```

If it's still unclear, try with a few more values in your REPL or on [Repl.it](https://repl.it/@valeriecodes/ModuloExamples).

Modular arithmetic comes up in plenty of applications irl, like minutes past the hour.

So we can see that numbers that are divisible by the second number will return zero with the modulus operator.

For numbers that are divisible by five:
``` py
i % 5 == 0
```

And for numbers that are divisible by three:
``` py
i % 3 == 0
```

Now that we have an easy way to identify these numbers, we can iterate through and add each of them to a running total. I wrote a function that would sum all 3 and 5 multiples under _n_, but there are plenty of alternate ways to approach it.

Here's my completed code, also available on [GitHub](https://github.com/valeriecodes/project-euler/blob/main/1.py).

``` py
def sumOf3or5MultiplesUnder(n):
    total = 0
    for i in range (0, n):
        if i % 3 == 0 or i % 5 == 0:
            total = total + i
    return total

print(sumOf3or5MultiplesUnder(1000))
```

That's it for problem one! Let me know what you think or if you used a different approach.