DEV Community

Cover image for Sum of 3 or 5 Multiples - Problem => Solution
Noman Gul
Noman Gul

Posted on

Sum of 3 or 5 Multiples - Problem => Solution

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


Solutions

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

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.

// initial array range/length

const numRange = 1000;

// Empty Array with 1000 undefined items

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

2.

// 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.

// 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.

// 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.

let sum = 0;

// loop max range

const numRange = 1000;

// common util function for checking multiples

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

2.

// 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.

// 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


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 and GitHub @NomanGul


More from Noman šŸ¦„

Top comments (3)

Collapse
 
gnsp profile image
Ganesh Prasad

This can be solved way more efficiently without using arrays or loops, if we approach it mathematically.

Let's assume:

  • sum of multiples of 3 or 5 below 1000 is a
  • sum of multiples of 3 below 1000 is b
  • sum of multiples of 5 below 1000 is c
  • sum of multiples of 3 and 5 (that is, multiples of 15) below 1000 is d

then, clearly, a = b + c - d
To find, a, we can start with finding the values of b, c and d.

To solve this, let's write find an algorithm to find the multiples of x below y.

  • The number of multiples of x (strictly) below y is n = Math.floor((y-1) / x)
  • The smallest multiple of x is always x.
  • The multiples of x below y form an Arithmetic Progression (AP), x, 2x, 3x ... nx
  • Sum of the AP is s = x + 2x + 3x + ... + nx
  • => s = x * (1 + 2 + 3 + ... + n )
  • => s = x * n * (n + 1) / 2

Let's write a function for that,

function sumOfMultiplesBelow (x, y) {
    const n = Math.floor((y-1) / x);
    return x * n * (n + 1) / 2;
}

const b = sumOfMultiplesBelow(3, 1000);
const c = sumOfMultiplesBelow(5, 1000);
const d = sumOfMultiplesBelow(15, 1000);

const a = b + c - d;
console.log(a);

This way, we solve the problem in O(1) time and space complexity.

Collapse
 
bugb profile image
bugb

My solution:

[...Array(1000).keys()].reduce((m,n)=>m+n*!!(n%3+n%5))/2+1|0

// 233168

Collapse
 
nomangul profile image
Noman Gul

Woooah... šŸ˜³ Are you Einstein?