Last active
September 20, 2017 22:47
-
-
Save kayslay/058b329f4138e3324201a37a33c29775 to your computer and use it in GitHub Desktop.
Calculates the sum of all multiples of numbers in an array between a limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description removes the multiples in the array | |
* @param arr | |
* @returns {Number} | |
*/ | |
function notMultiples(arr) { | |
arr = arr.sort((a, b) => a - b); | |
return arr.reduce((notMul, num) => { | |
if (notMul.length) { | |
if ((notMul.some(div => (num % div === 0)))) { | |
return notMul | |
} | |
notMul.push(num); | |
return notMul | |
} | |
return [num] | |
}, 0) | |
} | |
/** | |
* @description finds the sum of all Numbers tll n | |
* (n(n*1))/2 sum of natural Numbers till n | |
* @param n the nth sum we want to get | |
* @returns {Number} sum | |
*/ | |
function sumOfN(n) { | |
"use strict"; | |
return (n * (n + 1)) / 2 | |
} | |
/** | |
* @description finds the products of all the Numbers in the array | |
* NOTE: does not include the square of the Numbers | |
* | |
* @param array | |
* @returns {Array} | |
*/ | |
function multipleAll(array) { | |
let arr = [...array]; | |
let arrx = []; | |
let x = arr.shift(); | |
while (arr.length) { | |
let initArr = arr.map(val => x * val); | |
x = arr.shift(); | |
arrx = arrx.concat(initArr) | |
} | |
return arrx | |
} | |
/** | |
* @description sum of natural Numbers till n multiplied by value | |
* @param val value | |
* @param n the nth sum we want to get | |
* @returns {Number} | |
*/ | |
function sumOfNMultipliedByVal(val, n) { | |
"use strict"; | |
return sumOfN(n) * val | |
} | |
/** | |
* @description the sum of all multiples in the array btw the num + 1 | |
* @param {Number} num | |
* @param {Array} arr | |
* @returns {Number} | |
*/ | |
function sumOfAllNumbers(num, arr) { | |
const dividedByN = arr.map(n => Math.floor(num / n)); | |
const sumOfAllNInArr = arr.map((n, i) => sumOfNMultipliedByVal(n, dividedByN[i])); | |
return sumOfAllNInArr.reduce((acc, n) => acc + n,0); | |
} | |
/** | |
* @description finds the sum of the multiple less than the limit | |
* @param {Array} arr an array of the Number which multiple we want to get the sum | |
* @param {Number} lessThan | |
* @returns {Number} | |
*/ | |
function sumOfMultplesOfNumbersLessThan(arr, lessThan) { | |
const limit = lessThan - 1; | |
const filteredMul = notMultiples(arr); | |
const excessesArr = multipleAll(filteredMul); | |
const sum1 = sumOfAllNumbers(limit, filteredMul); | |
const sum2 = sumOfAllNumbers(limit, excessesArr); | |
return sum1 - sum2; | |
} | |
module.exports = sumOfMultplesOfNumbersLessThan; | |
//test: find the sum of the multiples of 3 and 5 less than 1000 | |
console.log(sumOfMultplesOfNumbersLessThan([3,5], 1000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calculates the sum of multiple of numbers in array less than a given number