Project Euler: Problem 1 with Javascript
Jared

Jared @codenutt

About: L.A. based web developer slowly parsing through Stack Overflow. If you like hot web dev tips or stories about being a freelancer, check out my newsletter: https://codenutt.substack.com/p/coming-soon

Location:
Los Angeles
Joined:
Sep 20, 2019

Project Euler: Problem 1 with Javascript

Publish Date: Jan 16 '20
30 2

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

Let's break it down in the code. Again, this is very verbose.

    function multiplesOf3and5(number) {
        // establish a global total and set initial value to 0
        let total = 0
        // loop through all values from 0 to given number   
      for(let i = 0; i <= number; i++) {
            // Get remainder of i and 3
            let remainderFor3 = i % 3;
            // Get remainder of i and 5
            let remainderFor5 = i % 5;

            // check if remainder for 3 or 5
            if(remainderFor3 == 0 || remainderFor5 == 0) {
                // If true, that means i is a multiple of 3
                // add it to the total
                total = total + i
            }
        }
        // return our total number
        return total
    }

Explanation of Modulo %

This line here is doing something interesting:

    i % 3

The operator in the middle is called a Modulus. It returns the remainder of two numbers. We can use it to see if one number is an even multiple of another number.

Refactor

We can reduce the code quite a lot, without losing the context of what we are trying to do. Here is my final solution.

    function multiplesOf3and5(number) {
      let total = 0;
      for (let i = 0; i <= number; i++) {
        if (i % 3 == 0 || i % 5 == 0) {
          total += i;
        }
      }
      return total;
    }

Final Thoughts

There is always room for improvement, however I like how this is setup. It's simple and doesn't abstract too far from what we are trying to accomplish. I'm certain if I come back to this in two years, I'll still know what's going on.

If you'd like to see the code, check out my Github repo with the problems I've completed so far.

DarthOstrich/projectEuler

Resources

Introducing The #ProjectEuler100 Challenge: the "Dark Souls" of Coding Achievements

Comments 2 total

  • Ajeeb.K.P
    Ajeeb.K.PMar 12, 2020

    Even though, they(projecteuler.net/) don't have strict rules. They don't encourage posting solutions to web.

    • Jared
      JaredMar 12, 2020

      The Project Euler 100 official twitter has retweeted all of my solutions so far, so I think it's alright. It encourages discussion.

Add comment