Finding the sum of a series.
Author |
Message |
AviaryPhoenix
|
Posted: Sun Feb 27, 2011 7:00 pm Post subject: Finding the sum of a series. |
|
|
Hi, I am quite new to the Turing software. I have several codes for sequences and counting. However, I need to write a program that will total all the numbers from 1 to 100.
Any help is appreciated. Thank You |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Sun Feb 27, 2011 7:06 pm Post subject: RE:Finding the sum of a series. |
|
|
What happened to the template form for new posts? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
AviaryPhoenix
|
Posted: Sun Feb 27, 2011 7:13 pm Post subject: Re: RE:Finding the sum of a series. |
|
|
Tony @ Sun Feb 27, 2011 7:06 pm wrote:
What happened to the template form for new posts?
Oh was i supposed to keep that?
Anyways I know the sum of the series from 1-100 is 5050, but is there any program that would total the series, no matter what numbers are used? |
|
|
|
|
|
Tony
|
Posted: Sun Feb 27, 2011 7:21 pm Post subject: RE:Finding the sum of a series. |
|
|
Yes, there is such a program. You should be able to write it. You could ask Carl Friedrich Gauss for advice, if you want to make it super fast. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
mirhagk
|
Posted: Sun Feb 27, 2011 7:43 pm Post subject: RE:Finding the sum of a series. |
|
|
I love you tony. LOL.
Guass is honestly my favourite mathematician ever.
I believe the story goes that his teacher wanted to punish him by forcing him to add all the numbers from 1 to 100 by hand. He realized that if you take 100 and 1 and add them it's 101. 2 and 99 also equal 101, same for 3 and 98, 4 and 97 etc...
So he took the first and last element and added them together 50 times (since you use 2 numbers each time). So it works out to (first number+last number)*number of numbers/2
There is also a mathematical proof proving this requiring surprisingly little algabra if you are interested. |
|
|
|
|
|
A.J
|
Posted: Sun Feb 27, 2011 9:26 pm Post subject: Re: Finding the sum of a series. |
|
|
There are two proofs actually, one involving telescoping the series (n+1)^2 - n^2, and the other involving writing the numbers in increasing and decreasing order to show Gauss' method. However, telescoping is a useful method that you can use to find the closed form of the sum of the first n powers of k.
So, to get the sum of the first n numbers, the telescoping series method would be as follows:
Consider the following summation:
sum(k = 1 -> n) [(k+1)^2 - k^2] = (n+1)^2 - 1 (since everything except for (n+1)^2 and -1^2 cancels).
However, (k+1)^2 - k^2 = 2k + 1. So:
sum (k = 1 -> n) [2k + 1] = (n+1)^2 - 1, or:
2*sum(k = 1 -> n) [k] + n = (n+1)^2 - 1, and thus sum(k = 1 -> n) [k] = n*(n+1)/2
So, in general, considering the summation sum(k = 1 -> n) [(k+1)^r - k^r] will yield the closed form for the sum of the first n powers r.
But to answer your original question, AviaryPhoenix, to compute the sum of the first n numbers, you either use the closed form derived above, or you try iterating through the numbers from 1 to n using a for loop, adding the values to some variable. |
|
|
|
|
|
|
|