For Loop and SumWhy should I avoid the For loop in Mathematica?How to solve a problem in relative...

How to prepare vegetables for a sandwich that can last for several days in a fridge?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

Do any poskim exempt 13-20-year-olds from Mussaf?

How do I make a gun alignment to camera matrix in OpenGL GLSL?

Why does the DC-9-80 have this cusp in its fuselage?

Eww, those bytes are gross

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

4 Spheres all touching each other??

Why zero tolerance on nudity in space?

Can the Count of Monte Cristo's calculation of poison dosage be explained?

Which branches of mathematics can be done just in terms of morphisms and composition?

How to approximate rolls for potions of healing using only d6's?

Two functions in the same line

Crystal compensation for temp and voltage

LTSpice: When running a linear AC simulation, how to view the voltage ratio between two voltages?

How can I improve my fireworks photography?

Is 45 min enough time to catch my next flight

What is Crew Dragon approaching in this picture?

Am I a Rude Number?

Can the SpaceX Dragon 2 crew vehicle still use the draco and super draco thrusters to slow down when landing?

Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?

What is the purpose of easy combat scenarios that don't need resource expenditure?

Criticizing long fiction. How is it different from short?

Can I retract my name from an already published manuscript?



For Loop and Sum


Why should I avoid the For loop in Mathematica?How to solve a problem in relative motion?FindFit giving weird values on a logistic equationReplace notebooks and pens with Mathematica?Module that prints first m positive odd integers and returns the square root of the sum of themTrouble defining function for homeworkVariable Increments in a TableWhat is p[a] forInfinite While loopAn efficient way to translate a triple for-loop from Matlab to MathematicaPlotting gradient and newton directions for parametric system of nonlinear ODEs













1












$begingroup$


I am requested to do a sum of the first fifty positive even numbers by using For loop.
The For loop that I created is



For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]


The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created



Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]] 


However, it is not a correct input. I am not sure if I should write the Sum inside the For loop.










share|improve this question











$endgroup$








  • 2




    $begingroup$
    If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
    $endgroup$
    – John Doty
    3 hours ago










  • $begingroup$
    If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
    $endgroup$
    – mjw
    1 hour ago












  • $begingroup$
    Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
    $endgroup$
    – MarcoB
    35 mins ago


















1












$begingroup$


I am requested to do a sum of the first fifty positive even numbers by using For loop.
The For loop that I created is



For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]


The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created



Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]] 


However, it is not a correct input. I am not sure if I should write the Sum inside the For loop.










share|improve this question











$endgroup$








  • 2




    $begingroup$
    If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
    $endgroup$
    – John Doty
    3 hours ago










  • $begingroup$
    If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
    $endgroup$
    – mjw
    1 hour ago












  • $begingroup$
    Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
    $endgroup$
    – MarcoB
    35 mins ago
















1












1








1





$begingroup$


I am requested to do a sum of the first fifty positive even numbers by using For loop.
The For loop that I created is



For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]


The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created



Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]] 


However, it is not a correct input. I am not sure if I should write the Sum inside the For loop.










share|improve this question











$endgroup$




I am requested to do a sum of the first fifty positive even numbers by using For loop.
The For loop that I created is



For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]


The output of this is a list of numbers from 2 to 50 and they are increasing by 2. Now I need these numbers to be sum so I created



Sum[For[natNum = 2, natNum <= 50, natNum = natNum + 2, Print[natNum]]] 


However, it is not a correct input. I am not sure if I should write the Sum inside the For loop.







homework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 41 mins ago









MarcoB

36.6k556112




36.6k556112










asked 3 hours ago









Laura FigueroaLaura Figueroa

61




61








  • 2




    $begingroup$
    If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
    $endgroup$
    – John Doty
    3 hours ago










  • $begingroup$
    If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
    $endgroup$
    – mjw
    1 hour ago












  • $begingroup$
    Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
    $endgroup$
    – MarcoB
    35 mins ago
















  • 2




    $begingroup$
    If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
    $endgroup$
    – John Doty
    3 hours ago










  • $begingroup$
    If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
    $endgroup$
    – mjw
    1 hour ago












  • $begingroup$
    Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
    $endgroup$
    – MarcoB
    35 mins ago










2




2




$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
$endgroup$
– John Doty
3 hours ago




$begingroup$
If this is a class assignment, I'd suggest withdrawing from the class. For loops are almost never a good way to do anything in Mathematica.
$endgroup$
– John Doty
3 hours ago












$begingroup$
If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
1 hour ago






$begingroup$
If you want the first 50 positive even numbers, I believe that is {2, 4, ..., 100}. The sum is (2+100) + (4+98) + ... + (50 + 52) = 102 * 25 = 2550.
$endgroup$
– mjw
1 hour ago














$begingroup$
Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
$endgroup$
– MarcoB
35 mins ago






$begingroup$
Total@Range[2, 2*50, 2] would be pretty direct. Note also that Print prints its arguments, but does not return them (in fact, it returns Null).
$endgroup$
– MarcoB
35 mins ago












2 Answers
2






active

oldest

votes


















3












$begingroup$

Anyone asking you to write For loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:



rslt = 0;
For[i = 0, i <= 50, i += 2, rslt += i];
rslt


And here's how someone with some familiarity with Mathematica might write it



Plus @@ Range[25]*2


Now, spend the time you were going to waste writing a For loop by reading the answers to this question Why should I avoid the For loop in Mathematica?






share|improve this answer









$endgroup$





















    0












    $begingroup$

    The first fifty positive integers can be computed with Table:



    Q = 0; (* Initialization)
    T = Table[Q + k, {k, 2 Range[50]}]


    enter image description here



    We can sum up the values of T with Sum:



    Sum[T[[k]], {k, Range[50]}]


    2550






    share|improve this answer









    $endgroup$













    • $begingroup$
      Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
      $endgroup$
      – MarcoB
      36 mins ago











    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.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192541%2ffor-loop-and-sum%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3












    $begingroup$

    Anyone asking you to write For loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:



    rslt = 0;
    For[i = 0, i <= 50, i += 2, rslt += i];
    rslt


    And here's how someone with some familiarity with Mathematica might write it



    Plus @@ Range[25]*2


    Now, spend the time you were going to waste writing a For loop by reading the answers to this question Why should I avoid the For loop in Mathematica?






    share|improve this answer









    $endgroup$


















      3












      $begingroup$

      Anyone asking you to write For loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:



      rslt = 0;
      For[i = 0, i <= 50, i += 2, rslt += i];
      rslt


      And here's how someone with some familiarity with Mathematica might write it



      Plus @@ Range[25]*2


      Now, spend the time you were going to waste writing a For loop by reading the answers to this question Why should I avoid the For loop in Mathematica?






      share|improve this answer









      $endgroup$
















        3












        3








        3





        $begingroup$

        Anyone asking you to write For loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:



        rslt = 0;
        For[i = 0, i <= 50, i += 2, rslt += i];
        rslt


        And here's how someone with some familiarity with Mathematica might write it



        Plus @@ Range[25]*2


        Now, spend the time you were going to waste writing a For loop by reading the answers to this question Why should I avoid the For loop in Mathematica?






        share|improve this answer









        $endgroup$



        Anyone asking you to write For loops in Mathematica for such a problem is a dolt. Nevertheless, here's how you might do that:



        rslt = 0;
        For[i = 0, i <= 50, i += 2, rslt += i];
        rslt


        And here's how someone with some familiarity with Mathematica might write it



        Plus @@ Range[25]*2


        Now, spend the time you were going to waste writing a For loop by reading the answers to this question Why should I avoid the For loop in Mathematica?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        High Performance MarkHigh Performance Mark

        528411




        528411























            0












            $begingroup$

            The first fifty positive integers can be computed with Table:



            Q = 0; (* Initialization)
            T = Table[Q + k, {k, 2 Range[50]}]


            enter image description here



            We can sum up the values of T with Sum:



            Sum[T[[k]], {k, Range[50]}]


            2550






            share|improve this answer









            $endgroup$













            • $begingroup$
              Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
              $endgroup$
              – MarcoB
              36 mins ago
















            0












            $begingroup$

            The first fifty positive integers can be computed with Table:



            Q = 0; (* Initialization)
            T = Table[Q + k, {k, 2 Range[50]}]


            enter image description here



            We can sum up the values of T with Sum:



            Sum[T[[k]], {k, Range[50]}]


            2550






            share|improve this answer









            $endgroup$













            • $begingroup$
              Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
              $endgroup$
              – MarcoB
              36 mins ago














            0












            0








            0





            $begingroup$

            The first fifty positive integers can be computed with Table:



            Q = 0; (* Initialization)
            T = Table[Q + k, {k, 2 Range[50]}]


            enter image description here



            We can sum up the values of T with Sum:



            Sum[T[[k]], {k, Range[50]}]


            2550






            share|improve this answer









            $endgroup$



            The first fifty positive integers can be computed with Table:



            Q = 0; (* Initialization)
            T = Table[Q + k, {k, 2 Range[50]}]


            enter image description here



            We can sum up the values of T with Sum:



            Sum[T[[k]], {k, Range[50]}]


            2550







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            mjwmjw

            3566




            3566












            • $begingroup$
              Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
              $endgroup$
              – MarcoB
              36 mins ago


















            • $begingroup$
              Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
              $endgroup$
              – MarcoB
              36 mins ago
















            $begingroup$
            Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
            $endgroup$
            – MarcoB
            36 mins ago




            $begingroup$
            Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T.
            $endgroup$
            – MarcoB
            36 mins ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192541%2ffor-loop-and-sum%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            IEEEtran - How to include ORCID in TeX/PDF with PdfLatexIs there a standard way to include ORCID in TeX /...

            Cicindela nigrior Przypisy | Menu nawigacyjneCicindela varians unicolorManual for the Identification of the...

            Glossaries-extra: Adding glossaries package to “Clas­sicTh­e­sis” template by Dr. André Miede v. 4.6 ...