mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
Update Problem001.js (#1536)
* Update Problem001.js * Update Problem001.js * Update Problem001.js * Update Problem001.js * Update Problem001.js
This commit is contained in:
@ -1,20 +1,23 @@
|
|||||||
// https://projecteuler.net/problem=1
|
// https://projecteuler.net/problem=1
|
||||||
/* Multiples of 3 and 5
|
/* 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.
|
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.
|
Find the sum of all the multiples of 3 or 5 below the provided parameter value number.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// This method uses the nSum function to add the nSum for 3 and 5. However, it needs to subtract the nSum for 15 once to avoid double counting.
|
||||||
const multiplesThreeAndFive = (num) => {
|
const multiplesThreeAndFive = (num) => {
|
||||||
if (num < 1) throw new Error('No natural numbers exist below 1')
|
if (num < 1) throw new Error('No natural numbers exist below 1')
|
||||||
|
num -= 1
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
let total = 0
|
// The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num.
|
||||||
// total for calculating the sum
|
// Here, n is denoted as frequency.
|
||||||
for (let i = 1; i < num; i++) {
|
const nSum = (num, frequency) => (frequency * (frequency + 1) * num) >> 1
|
||||||
if (i % 3 === 0 || i % 5 === 0) {
|
|
||||||
total += i
|
sum += nSum(3, Math.floor(num / 3))
|
||||||
}
|
sum += nSum(5, Math.floor(num / 5))
|
||||||
}
|
sum -= nSum(15, Math.floor(num / 15))
|
||||||
return total
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
export { multiplesThreeAndFive }
|
export { multiplesThreeAndFive }
|
||||||
|
Reference in New Issue
Block a user