Sum of Multiples of 3 or 5 up to 1000
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Question:
Find the sum of multiples of 3 or 5 up to 1000
I have come up with this solution that is bit more generic than the first 1000 per se:
let multiplesSum = n =>
let sum = 0
for (let i = 0; i < n; i++)
return sum
Instead of pushing
everything to an array use a variable and sum the numbers as they are looping.
javascript performance programming-challenge
add a comment |Â
up vote
1
down vote
favorite
Question:
Find the sum of multiples of 3 or 5 up to 1000
I have come up with this solution that is bit more generic than the first 1000 per se:
let multiplesSum = n =>
let sum = 0
for (let i = 0; i < n; i++)
return sum
Instead of pushing
everything to an array use a variable and sum the numbers as they are looping.
javascript performance programming-challenge
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
3
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Question:
Find the sum of multiples of 3 or 5 up to 1000
I have come up with this solution that is bit more generic than the first 1000 per se:
let multiplesSum = n =>
let sum = 0
for (let i = 0; i < n; i++)
return sum
Instead of pushing
everything to an array use a variable and sum the numbers as they are looping.
javascript performance programming-challenge
Question:
Find the sum of multiples of 3 or 5 up to 1000
I have come up with this solution that is bit more generic than the first 1000 per se:
let multiplesSum = n =>
let sum = 0
for (let i = 0; i < n; i++)
return sum
Instead of pushing
everything to an array use a variable and sum the numbers as they are looping.
javascript performance programming-challenge
edited Jan 27 at 3:14
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jan 27 at 1:51
ukaric
64
64
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
3
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14
add a comment |Â
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
3
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
3
3
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
Implementation
Your implementation is clean, robust and easy to understand. I suggest only small changes:
- Declare
multiplesSum
asconst
to prevent accidental rebinding / overriding later on. multiplesSum(n)
should compute the sum of multiples of 3 or 5 up to and including n. Sum formulas are usually following this convention.- I prefer terminating statements with semicolons instead of relying on JavaScript's automatic semicolon insertion.
Algorithm
Your algorithm is simple and easy to understand, but comes with a linear runtime complexity of O(n)
.
An algorithm with constant runtime complexity O(1)
exists. Here are some pointers in case you are stuck:
- Given a positive integer n, how often do 3 or 5 fit into n? Division and
Math.floor
give the answer. - Arithmetic sequences such as 3 + 6 + 9 + ... have simple, explicit sum formulas.
- By adding (3 + 6 + 9 + ...) + (5 + 10 + 15 + ...), you count some numbers twice, so you need to find and subtract those.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Implementation
Your implementation is clean, robust and easy to understand. I suggest only small changes:
- Declare
multiplesSum
asconst
to prevent accidental rebinding / overriding later on. multiplesSum(n)
should compute the sum of multiples of 3 or 5 up to and including n. Sum formulas are usually following this convention.- I prefer terminating statements with semicolons instead of relying on JavaScript's automatic semicolon insertion.
Algorithm
Your algorithm is simple and easy to understand, but comes with a linear runtime complexity of O(n)
.
An algorithm with constant runtime complexity O(1)
exists. Here are some pointers in case you are stuck:
- Given a positive integer n, how often do 3 or 5 fit into n? Division and
Math.floor
give the answer. - Arithmetic sequences such as 3 + 6 + 9 + ... have simple, explicit sum formulas.
- By adding (3 + 6 + 9 + ...) + (5 + 10 + 15 + ...), you count some numbers twice, so you need to find and subtract those.
add a comment |Â
up vote
3
down vote
Implementation
Your implementation is clean, robust and easy to understand. I suggest only small changes:
- Declare
multiplesSum
asconst
to prevent accidental rebinding / overriding later on. multiplesSum(n)
should compute the sum of multiples of 3 or 5 up to and including n. Sum formulas are usually following this convention.- I prefer terminating statements with semicolons instead of relying on JavaScript's automatic semicolon insertion.
Algorithm
Your algorithm is simple and easy to understand, but comes with a linear runtime complexity of O(n)
.
An algorithm with constant runtime complexity O(1)
exists. Here are some pointers in case you are stuck:
- Given a positive integer n, how often do 3 or 5 fit into n? Division and
Math.floor
give the answer. - Arithmetic sequences such as 3 + 6 + 9 + ... have simple, explicit sum formulas.
- By adding (3 + 6 + 9 + ...) + (5 + 10 + 15 + ...), you count some numbers twice, so you need to find and subtract those.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Implementation
Your implementation is clean, robust and easy to understand. I suggest only small changes:
- Declare
multiplesSum
asconst
to prevent accidental rebinding / overriding later on. multiplesSum(n)
should compute the sum of multiples of 3 or 5 up to and including n. Sum formulas are usually following this convention.- I prefer terminating statements with semicolons instead of relying on JavaScript's automatic semicolon insertion.
Algorithm
Your algorithm is simple and easy to understand, but comes with a linear runtime complexity of O(n)
.
An algorithm with constant runtime complexity O(1)
exists. Here are some pointers in case you are stuck:
- Given a positive integer n, how often do 3 or 5 fit into n? Division and
Math.floor
give the answer. - Arithmetic sequences such as 3 + 6 + 9 + ... have simple, explicit sum formulas.
- By adding (3 + 6 + 9 + ...) + (5 + 10 + 15 + ...), you count some numbers twice, so you need to find and subtract those.
Implementation
Your implementation is clean, robust and easy to understand. I suggest only small changes:
- Declare
multiplesSum
asconst
to prevent accidental rebinding / overriding later on. multiplesSum(n)
should compute the sum of multiples of 3 or 5 up to and including n. Sum formulas are usually following this convention.- I prefer terminating statements with semicolons instead of relying on JavaScript's automatic semicolon insertion.
Algorithm
Your algorithm is simple and easy to understand, but comes with a linear runtime complexity of O(n)
.
An algorithm with constant runtime complexity O(1)
exists. Here are some pointers in case you are stuck:
- Given a positive integer n, how often do 3 or 5 fit into n? Division and
Math.floor
give the answer. - Arithmetic sequences such as 3 + 6 + 9 + ... have simple, explicit sum formulas.
- By adding (3 + 6 + 9 + ...) + (5 + 10 + 15 + ...), you count some numbers twice, so you need to find and subtract those.
answered Jan 27 at 15:24
le_m
1,700213
1,700213
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186105%2fsum-of-multiples-of-3-or-5-up-to-1000%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You should probably look at other solutions to Project Euler #1. There are lots out there.
â Oscar Smith
Jan 27 at 2:05
Yeah probably right idea. After looking into some of them I found small improvment.
â ukaric
Jan 27 at 2:16
3
I find the urge to solve this problem with a one-line math formula practically irresistable. No loops required.
â David K
Jan 27 at 3:14