Sum of all integers
How can I figure out the sum of all integers from 1 - 100? Is there a theorem for this?
You could use a loop
unsigned int total = 0;
int i = 0;
while(i < 101)
{
total = total + i;
i++;
}
cout << total;
or something like that
EDIT: That code reveals the awnser to be 5050
[edited by - googlyeyes on October 25, 2002 5:29:37 PM]
unsigned int total = 0;
int i = 0;
while(i < 101)
{
total = total + i;
i++;
}
cout << total;
or something like that
EDIT: That code reveals the awnser to be 5050
[edited by - googlyeyes on October 25, 2002 5:29:37 PM]
You know, you could try typing "sum of all integers" in Google. The third search result is exactly what you needed.
To explain what the others have said: multiply their average by how many there are.
Well, to get the average in the first place you''d have to add them all up anyway

Or you could just note that the average of an arithmetic series is equal to the average of the first and last elements

Gauss worked this out when he was four years old!
My favourite way of seeing the result is to imagine a "pyramid" of 1x1 squares, the first row has 1, the second 2, all the way up to row n. You rotate the pyramid 180 degrees and it fits nicely together with itself forming a rectangle of area
n( n + 1 )
X
XX
XXX
XXXX
add a 180 degree rotated pyramid to see a 5x4 rect...
XOOOO
XXOOO
XXXOO
XXXXO
The area of the sum is then half the above result. Not very rigorous though. A proper proof involves induction.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
My favourite way of seeing the result is to imagine a "pyramid" of 1x1 squares, the first row has 1, the second 2, all the way up to row n. You rotate the pyramid 180 degrees and it fits nicely together with itself forming a rectangle of area
n( n + 1 )
X
XX
XXX
XXXX
add a 180 degree rotated pyramid to see a 5x4 rect...
XOOOO
XXOOO
XXXOO
XXXXO
The area of the sum is then half the above result. Not very rigorous though. A proper proof involves induction.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement