3
$\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.

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

2 Answers 2

12
$\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?

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

The first fifty positive integers can be computed with Table:

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

output of the above code

We can sum up the values of T with Sum:

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

2550

$\endgroup$
4
  • 1
    $\begingroup$ Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T. $\endgroup$
    – MarcoB
    Commented Mar 3, 2019 at 22:56
  • $\begingroup$ 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! $\endgroup$
    – mjw
    Commented Mar 4, 2019 at 0:23
  • $\begingroup$ 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! $\endgroup$
    – MarcoB
    Commented Mar 4, 2019 at 1:34
  • $\begingroup$ 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 @@. $\endgroup$
    – mjw
    Commented Mar 4, 2019 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.