Sum of 3 or 5 Multiples - Problem => Solution
Tags: javascript, beginners, tutorial
Published: 2019-09-15 04:00:55

# Problem

Find the sum of all the multiples of 3 or 5 below 1000. e.g. 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.

![code problem](https://media.giphy.com/media/xT8qBsOjMOcdeGJIU8/giphy.gif)

---

# Solutions

We implement two solutions but first I want to mention common function in both solutions...

```js
const isMultiple = (num, mode) => (num % mode ? false : true);

// Checks the given number is multiple or not with the help of modulus

// e.g. isMultiple(6, 3) returns true because 6 % 3 remainder is 0.
// This mean 6 is multiple of 3.

// At the other hand isMultiple(26, 5) returns false
```

---

# With => Array Methods

1.

```js
// initial array range/length

const numRange = 1000;

// Empty Array with 1000 undefined items

const array1000 = [...Array(numRange)];
```

2.

```js
// array where we push 3 or 5 multiples

const threeOrFiveMultiples = [];

// common util function for checking multiples

const isMultiple = (num, mode) => (num % mode ? false : true);

// mapping over an undefined items array and pushing
// 3 or 5 multiples indexes to threeOrFiveMultiples array

array1000.map((_, index) => {
  if (isMultiple(index, 3) || isMultiple(index, 5))
    threeOrFiveMultiples.push(index);
});
```

3.

```js
// reducing the array to a single value

const sum = threeOrFiveMultiples.reduce((acc, curr) => {
  return acc + curr;
}, 0);

// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168

console.log(sum);
```

4.

```js
// complete solution with array methods

// initial array range/length
const numRange = 1000;

// Empty Array with 1000 undefined items
const array1000 = [...Array(numRange)];

// array where we push 3 or 5 multiples
const threeOrFiveMultiples = [];

// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);

// mapping over an undefined items array and pushing
// 3 or 5 multiples indexes to "threeOrFiveMultiples" array
array1000.map((_, index) => {
  if (isMultiple(index, 3) || isMultiple(index, 5))
    threeOrFiveMultiples.push(index);
});

// reducing the array to a single value
const sum = threeOrFiveMultiples.reduce((acc, curr) => {
  return acc + curr;
}, 0);

// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
```

---

# With => For Loop

1.

```js
let sum = 0;

// loop max range

const numRange = 1000;

// common util function for checking multiples

const isMultiple = (num, mode) => (num % mode ? false : true);
```

2.

```js
// looping over an numbers below 1000 and adding 3 or 5
// multiples to sum

for (let i = 0; i < numRange; i++) {
  if (isMultiple(i, 3) || isMultiple(i, 5)) sum += i;
}

// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168

console.log(sum);
```

3.

```js
// complete solution with For Loop

let sum = 0;

// loop max range
const numRange = 1000;

// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);

// looping over an numbers below 1000 and adding 3 or 5
// multiples to sum
for (let i = 0; i < numRange; i++) {
  if (isMultiple(i, 3) || isMultiple(i, 5)) sum += i;
}

// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
```

![problem solved](https://media.giphy.com/media/JozPUJqrzDjZRXCTI5/giphy.gif)

---

Let me know which solution you like more and why? (BTW my favorite one is with "For Loop" because it's so easy and less code)

_And, for more cool stuff, follow me on Twitter [@NomanGulKhan](https://twitter.com/NomanGulKhan) and GitHub [@NomanGul](https://github.com/NomanGul)_

---

<center><b>More from Noman 🦄</b></center>

{% link nomangul/full-stack-dev-explained-in-2-minutes-1amn %}
{% link nomangul/margin-right-50-4oko %}