3

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.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • 6
    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. – John Doty Mar 03 '19 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 Mar 03 '19 at 22:06
  • 1
    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). – MarcoB Mar 03 '19 at 22:57
  • 7
    Since the days of Carl Friedrich Gauß we know that using a For loop to compute 50 * 51 is a pretty bad idea.... – Henrik Schumacher Mar 03 '19 at 23:51
  • Is it the first fifty that you want, or is it {2, 4, ..., 50}? The original question had both. – mjw Mar 04 '19 at 02:56

2 Answers2

12

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?

High Performance Mark
  • 1,534
  • 1
  • 10
  • 17
3

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

mjw
  • 2,146
  • 5
  • 13
  • 1
    Perhaps better / more idiomatic than your Sum approach would be using Total[T] or Plus@@T. – MarcoB Mar 03 '19 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 Mar 04 '19 at 00: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! – MarcoB Mar 04 '19 at 01: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 Mar 04 '19 at 02:23