Sum of Multiples of 3 or 5 up to 1000

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question





















  • 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
















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.







share|improve this question





















  • 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












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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








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
















  • 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










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 as const 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.





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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 as const 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.





    share|improve this answer

























      up vote
      3
      down vote













      Implementation



      Your implementation is clean, robust and easy to understand. I suggest only small changes:



      • Declare multiplesSum as const 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.





      share|improve this answer























        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 as const 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.





        share|improve this answer













        Implementation



        Your implementation is clean, robust and easy to understand. I suggest only small changes:



        • Declare multiplesSum as const 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.






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jan 27 at 15:24









        le_m

        1,700213




        1,700213






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?