# For Loop and Sum

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.

• 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. Commented Mar 3, 2019 at 20:32
• 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.
– mjw
Commented Mar 3, 2019 at 22:06
• 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). Commented Mar 3, 2019 at 22:57
• Since the days of Carl Friedrich Gauß we know that using a For loop to compute 50 * 51 is a pretty bad idea.... Commented Mar 3, 2019 at 23:51
• Is it the first fifty that you want, or is it {2, 4, ..., 50}? The original question had both.
– mjw
Commented Mar 4, 2019 at 2:56

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?

• +1 for the first sentence. Commented Mar 3, 2019 at 23:49

The first fifty positive integers can be computed with Table:

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


We can sum up the values of T with Sum:

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


2550

• Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T. Commented Mar 3, 2019 at 22:56
• Thank you! I was looking for something like Total and I saw the Plus posting above, but haven't yet learned about @@ and such. I would have thought Map might work here (it does not). Anyway, yes Total[T] and Plus@@T are simpler and more elegant!
– mjw
Commented Mar 4, 2019 at 0:23
• I was a latecomer to @@ as well. When I was learning about it, it helped me to think about it as a way of "swapping heads" of expressions. So for instance, if you have {1, 2, 3}, its FullForm would be List[1, 2, 3]. If you then Apply Plus to it, i.e. Plus@@, you swap the Listhead for a Plus head, to give Plus[1, 2, 3]. Although it may seem like a niche application at first, once you get used to it, it is tremendously useful! Commented Mar 4, 2019 at 1:34
• This is great, thank you! Just as I learned not to use For loops (especially on this Forum), and to Do use Do, I'll look for opportunities to Apply @@.
– mjw
Commented Mar 4, 2019 at 2:23