Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1cc5e61

Browse files
Update Problem001.js (TheAlgorithms#1536)
* Update Problem001.js * Update Problem001.js * Update Problem001.js * Update Problem001.js * Update Problem001.js
1 parent e5af4c2 commit 1cc5e61

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

‎Project-Euler/Problem001.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// https://projecteuler.net/problem=1
2-
/* Multiples of 3 and 5
3-
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.
2+
/* Multiples of 3 and 5
3+
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.
44
Find the sum of all the multiples of 3 or 5 below the provided parameter value number.
55
*/
66

7+
// 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.
78
const multiplesThreeAndFive = (num) => {
89
if (num < 1) throw new Error('No natural numbers exist below 1')
10+
num -= 1
11+
let sum = 0
912

10-
lettotal=0
11-
// total for calculating the sum
12-
for(leti=1;i<num;i++){
13-
if(i%3===0||i%5===0){
14-
total+=i
15-
}
16-
}
17-
return total
13+
// The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num.
14+
// Here, n is denoted as frequency.
15+
constnSum=(num,frequency)=>(frequency*(frequency+1)*num)>>1
16+
17+
sum+=nSum(3,Math.floor(num/3))
18+
sum+=nSum(5,Math.floor(num/5))
19+
sum-=nSum(15,Math.floor(num/15))
20+
return sum
1821
}
1922

2023
export { multiplesThreeAndFive }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /