I am looking for different ways to answer this problem?
Want to find the sum of positive integers from 0 to N.
For example if N = 5 then
SUM = 0+1+2+3+4+5.
How do I solve if N = 1000
I found some interesting ways but looking for more ways to do it.
Off the top of my head, you can do it with one multiplication. Middle-Value times N.
If N=5 then SUM = 1+2+3+4+5 = 15, using my idea the middle is 3, and 3 x 5 = 15
If N=6 then SUM = 1+2+3+4+5+6 = 21, using my idea the middle is 3.5 (half-way between 3 and 4) , and 3.5 x 6 = 21
This could be generalized as (Stt+End)/2 * (End-Stt+1)
where Stt=1 and End=N for your needs.
Let us try this one:
Using (Stt+End)/2 * (End-Stt+1): (3+6)/2 * (6-3+1) = 4.5 * 4 = 18
For your N=1000 case we would have (1+1000)/2 * (1000-1+1) = 500.5 * 1000 = 500,500
"The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman
I have learned this formula before. It is connected with mathematical induction. German mathematician Gauss, when he was 7 years old, thought of this formula. His teacher told students to find the sum of numbers, starting from 1 to 100. He thought that students won't finish it in next 30 minutes, but Gauss did. n(n+1)/2 where n is the last number.
This formula can be generalised so you can start from any number you like that belongs to N:
since n(n+1)/2 is when you start from 1, we will subtract the sum that needs to be subtracted:
if you are starting from 3 you will subtract from the main formula, 1+2. So let's write it:
n(n+1)/2 - x(x+1)/2
x=number below the one you are starting from
st= starting number
n(n+1)/2 - (st-1)(st-1+1)/2= [n(n+1)-st(st-1)]/2=[n^2+n-st^2+st]/2=[(n-st)(n+st)+n+st]/2=(n+st)(n-st+1)/2 -formula that administrator has mentioned
You also have formula for:
n e N 1^2+2^2+3^2.......n^2= n(n+1)(2n+1)/6