Remember Me?

 Straight Dope Message Board Remember Me?

#1
08-29-2016, 04:45 PM
 HeyHomie Charter Member Join Date: Sep 1999 Location: Viburnum, MO Posts: 9,317
I Need A Formula (Or A Calculator Function) For Adding A Series Of Consecutive Integers

I want to calculate the sum of the numbers 1+2+3+4 and so on. Is there a formula I can use? Does it work with any beginning and ending number (say, 23+24+25 and so on, up to, say 48)?

Is there a quick & easy way to do it on the calculator that came with my iPhone 4S?
#2
08-29-2016, 04:56 PM
 Schnitte Guest Join Date: Feb 2001 Location: Frankfurt, Germany Posts: 3,469
You can use the trick used by Gauss in a famous anecdote: You take the largest number in your series, add 1, and multiply that by half the number of integers in your series.

For instance (in Gauss' example), 1 + 2 + 3 + ... + 100 = (1 + 100) * (100/2) = 101 * 50 = 5050. It works because the lowest and the highest number can be paired to 1 + 100; the second lowest and second highest number to 2 + 99, also 101, and so on; you get 50 pairs, all adding up to 101.
__________________
"The banana has certain characteristics, appearance, taste, softness, seedlessness, easy handling, a constant level of production which enable it to satisfy the constant needs of an important section of the population consisting of the very young, the old and the sick."
#3
08-29-2016, 04:59 PM
 Schnitte Guest Join Date: Feb 2001 Location: Frankfurt, Germany Posts: 3,469
Oh, and for your example with 23 + 24 + ... + 48: You can apply the trick twice:

First step: Use that method to calculate the sum of all integers up to 48, which is 49 * 24 = 1,176.

Second step: Use the method to calculate the sum of all integers up to 22, which is 12 * 11 = 242.

Third step: Subtract the sum of the integers up to (and including) 22 from the sum of all integers up to and including 48; the difference, 934, is the number you're looking for.
#4
08-29-2016, 05:03 PM
 OldGuy Charter Member Join Date: Dec 2002 Location: Very east of Foggybog, WI Posts: 4,637
So in summary (pun intended) the formula for the sum of the integers from m to n is

[n*(n+1)-m*(m-1)]/2
#5
08-29-2016, 05:05 PM
 The Great Unwashed Guest Join Date: Mar 2002 Location: France Posts: 2,081
For consecutive integers, m... ...n, the formula is

(m+n)(m-n+1)/2
#6
08-29-2016, 05:10 PM
 Chronos Charter Member Moderator Join Date: Jan 2000 Location: The Land of Cleves Posts: 75,279
Another way of thinking of it: Take the average of the numbers (which, for consecutive integers, will just be the smallest plus the largest divided by two), and multiply it by the number of them.

Which of course will give you the same result as the other method.
#7
08-29-2016, 05:10 PM
 Derleth Guest Join Date: Apr 2000 Location: Missoula, Montana, USA Posts: 20,173
The most general term for the kind of thing you're looking for is "closed-form expression" or similar; these problems have no magical solution, but instead require some insight and pattern-matching to solve.

Here's a page on the subject which walks you through an example and provides some general advice.
#8
08-29-2016, 05:12 PM
 HeyHomie Charter Member Join Date: Sep 1999 Location: Viburnum, MO Posts: 9,317
Thanks! Having mathematicians around is helpful!
#9
08-29-2016, 05:29 PM
 Indistinguishable Guest Join Date: Apr 2007 Posts: 10,525
Quote:
 Originally Posted by Derleth The most general term for the kind of thing you're looking for is "closed-form expression" or similar; these problems have no magical solution, but instead require some insight and pattern-matching to solve. Here's a page on the subject which walks you through an example and provides some general advice.
In the greatest generality, any particular problem becomes the entirety of human knowledge and ignorance, every solved and unsolved potential problem agglomerated together into "How do I figure out everything?". But at least the OP's specific question does indeed have a "magical solution".

Last edited by Indistinguishable; 08-29-2016 at 05:34 PM.
#10
08-29-2016, 07:32 PM
 OldGuy Charter Member Join Date: Dec 2002 Location: Very east of Foggybog, WI Posts: 4,637
Quote:
 Originally Posted by The Great Unwashed For consecutive integers, m... ...n, the formula is (m+n)(m-n+1)/2
Be careful with this one. The formula assumes m > n, but the description would imply m < n.
#11
08-30-2016, 02:17 AM
 The Great Unwashed Guest Join Date: Mar 2002 Location: France Posts: 2,081
Quote:
 Originally Posted by OldGuy Be careful with this one. The formula assumes m > n, but the description would imply m < n.
Good call.

S = (m+n)(n-m+1)/2

 Bookmarks

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Main     About This Message Board     Comments on Cecil's Columns/Staff Reports     General Questions     Great Debates     Elections     Cafe Society     The Game Room     Thread Games     In My Humble Opinion (IMHO)     Mundane Pointless Stuff I Must Share (MPSIMS)     Marketplace     The BBQ Pit

All times are GMT -5. The time now is 04:52 PM.

 -- Straight Dope v3.7.3 -- Sultantheme's Responsive vB3-blue Contact Us - Straight Dope Homepage - Archive - Top

Send questions for Cecil Adams to: cecil@straightdope.com