-
September 30th, 2003, 11:08 PM
#1
Total newbie question
Could someone show me how to add the numbers 1 thru 50 and display the total of 1275 the easy way. My brain wont close in on this one and I dont want to use a ton of variables.
Thanks in advance
trip7
-
September 30th, 2003, 11:14 PM
#2
you mean something like this?
long count = 0;
for( long i = 0; i <= 50; i++)
count += i;
-
September 30th, 2003, 11:16 PM
#3
Code:
int result = 0;
for(int i = 0 ; i < 51 ; i ++ )
{
result += i ;
}
thats it...
'result' is 1275
R. Thomas
"Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18
-
September 30th, 2003, 11:22 PM
#4
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
September 30th, 2003, 11:23 PM
#5
Exactly what I was looking for and thanks for your help all!
-
September 30th, 2003, 11:24 PM
#6
P.S.
in general the sum of the numbers from 1 to n is
sum = n*(n+1)/2
and in general to sum the numbers from m to n with 1 <= m < n
sum = (m+n)(m-n+1)/2
proof
write the sum in two ways
sum = m + (m+1) + (m+2) +...+ (n-2) + (n-1) + n
sum = n + (n - 1) + (n - 2)+...+ (m+2) + (m+1) + m
Now add the columns
2*sum = (m+n)+(m+n)+(m+n)+...+(m+n)+(m+n)+(m+n)
How many terms are on the right? (m-n+1)
so
2*sum = (m+n)(m-n+1)
and it done
Last edited by souldog; September 30th, 2003 at 11:40 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
September 30th, 2003, 11:25 PM
#7
R. Thomas
"Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18
-
September 30th, 2003, 11:37 PM
#8
Of course, there is always a good way and an excellent way of doing something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|