Total newbie question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Total newbie question

  1. #1
    Join Date
    Sep 2003
    Location
    Seattle, WA
    Posts
    16

    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

  2. #2
    Join Date
    Sep 2003
    Location
    So Cal
    Posts
    7
    you mean something like this?


    long count = 0;

    for( long i = 0; i <= 50; i++)
    count += i;

  3. #3
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    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

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    sum = (50*51)/2= 25*51
    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."

  5. #5
    Join Date
    Sep 2003
    Location
    Seattle, WA
    Posts
    16
    Exactly what I was looking for and thanks for your help all!

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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; October 1st, 2003 at 12:40 AM.
    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."

  7. #7
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by souldog
    sum = (50*51)/2= 25*51
    well i alwasys thought...there is a vast difference between experience+coolness and education...

    cool Mr.Souldog....expected a cool answer from you like above..




    now comes the formula.....cool!!!!
    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

  8. #8
    Join Date
    Sep 2003
    Location
    So Cal
    Posts
    7
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center