codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Project Euler: Problem 1 with Javascript

Jared Nutt
codeburst
Published in
3 min readJan 18, 2020

Multiples of 3 and 5

Solving Project Euler’s 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.

  1. Given a number, see if it is a multiple of 3
  2. If true, add it to a total number
  3. Given a number, see if it is a multiple of 5
  4. If true, add it to a total number

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Jared Nutt

Freelance web developer living in Los Angeles. I write articles I wish I had when I was learning — mostly about Javascript and web development.

Responses (5)

Write a response