1. ## addition / recursion problem

If you wanted to add all numbers together from 1 to x (ex. 1+2+3+4+5...x) what is the best way? Mathmatically there are ways easier than adding them all up in order, but I am curious about the programming side. Are there better ways to do this problem besides the obvious answers of a) one loop that takes forever or b) the recursion solution?

2. Not that you should listen to me (I seem to have a problem with Basic Algebra), but I think this will work:

((n + 1) / 2) * ((n - 1) + 1)

or just:

((n + 1) / 2) * n

Incidentally, this should work for a consecutive series between any two numbers. Just replace the inner 1's with the smaller number. For example:

To add the series 4+5+6+7+8+9+10, the equation would be:

((10+4) / 2) * ((10 - 4) + 1)

Which should equal 49...the same answer to 4+5+6+7+8+9+10. Again, I think this will always work, but I'm not sure.

The algebraic equation to add all numbers between x and n:

((n + x) / 2) * ((n - x) + 1)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•