Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

How do I obtain the sum of all iterations of a loop?

Posted 6 years ago
POSTED BY: Chris Agee
2 Replies

Chris,

Mathematica (MMA) is a list based language and looping is generally slow and "clunky" in MMA. You should try to formulate using lists.

  1. To fix the kernel reset each time, just put a Clear[x]; before your loop (but I would not use a loop).

  2. Use Table instead

    result = Table[c,{x,1,17,2}]
    

This creates a list of c values for x's ranging from 1 to 17 incrementing by 2.

To total it you can create a list of Accumulations or use Total

Accumulate[result]

{3, 10, 21, 36, 55, 78, 105, 136, 171}

Or Total

Total[result]
171

Regards,

Neil

POSTED BY: Neil Singer
Posted 6 years ago

Thank you for the quick response, I was so myopically focused on using loops, that a table never even occurred to me! This is a much better solution, thanks!

POSTED BY: Chris Agee
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard