FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Project-Euler/Problem001.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Project-Euler
/
Problem001.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
23 lines (19 loc) · 956 Bytes
Breadcrumbs
JavaScript
/
Project-Euler
/
Problem001.js
Copy path
File metadata and controls
23 lines (19 loc) · 956 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// https://projecteuler.net/problem=1
/* 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.
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
)
=>
{
if
(
num
<
1
)
throw
new
Error
(
'No natural numbers exist below 1'
)
num
-=
1
let
sum
=
0
// The nSum function calculates the sum of the first n numbers in the sequence with a common difference of num.
// Here, n is denoted as frequency.
const
nSum
=
(
num
,
frequency
)
=>
(
frequency
*
(
frequency
+
1
)
*
num
)
>>
1
sum
+=
nSum
(
3
,
Math
.
floor
(
num
/
3
)
)
sum
+=
nSum
(
5
,
Math
.
floor
(
num
/
5
)
)
sum
-=
nSum
(
15
,
Math
.
floor
(
num
/
15
)
)
return
sum
}
export
{
multiplesThreeAndFive
}
Back
|
FazBrowse Home
|
New Git URL