Member-only story
Project Euler: Problem 1 with Javascript
Multiples of 3 and 5

Front Matter
Here we are, attempting the Dark Souls of coding challenges. We’ll start today with a fairly simple one: getting multiples of 3 and 5.
Problem 1: Multiples of 3 and 5
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 the provided parameter value number
.
Video Version
If you like to watch rather than read, check out the video that accompanies this article. If not, keep reading!
Solution
At first glance, this seems more complicated than it actually is. For the purpose of learning, I am going to be as verbose as possible, then refactor later.
Breakdown of the Problem in Plain English
It’s important to break down all the elements of the problem, so we fully understand what we are trying to do.
Natural Number
the positive integers (whole numbers) 1, 2, 3, etc., and sometimes zero as well.
Multiple of x
When we say,
“is 6 a multiple of 3?”
we are asking,
“is 6 a number that can be calculated by multiplying 3 by a number (in our case whole numbers)”
In this case, yes, 3 x 2 = 6.
Steps
Now that we understand our problem, let’s make some logical statements.
- Given a number, see if it is a multiple of 3
- If true, add it to a total number
- Given a number, see if it is a multiple of 5
- If true, add it to a total number