Results 1 to 16 of 16

Thread: 1 to 100 ...

  1. #1

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96

    Talking 1 to 100 ...

    alright, so i need to make a pattern that will show "the sum of 1 to 100: it will display the number in which is the total of the numbers from 1 to 100." here's what i tried, it did not work... maybe you someone can help me out ..

    Dim p As Double
    intp = 1
    intw = 0
    For intp = 0 To 100
    lstDisplay.AddItem intp + intw
    intw = intw + 1
    Next
    you'll feel better when you cannot feel at all ...

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    Re: 1 to 100 ...

    Sum of 1 to 100?

    You mean 100? Or.. 1+2+3.. etc to 100.

    (ubound-lbound)+1 = range(count)

  3. #3

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96
    i mean the second option you gave, 1+2+3 ...ect to 100 ...

    what is wrong with my coding?
    you'll feel better when you cannot feel at all ...

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking ok...

    1) You are adding a thing to the list each time, not just the sum at the end.
    2) You are not storing the intermediate sums anywhere. I'm guessing you want them in intw, in that case you should have this instead:
    VB Code:
    1. intw = 0
    2. For intp = 0 to 100
    3. intw = intw + intp    '*****
    4. Next
    5. lstDisplay.AddItem intw
    Note that if you want ALL intermediate sums added to the list box, move the .AddItem call to after the line with the *****
    sql_lall

  5. #5

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96
    so it should be 5050?... thanks so much.
    you'll feel better when you cannot feel at all ...

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    101 * 50 = 5050
    I don't live here any more.

  7. #7

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96
    haha so it is right?
    you'll feel better when you cannot feel at all ...

  8. #8
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    if you need the final sum only use
    (a+l)*n/2, where
    a: is starting value (here 1)
    l: is last value (here 100)
    n: is count of values ( here 100 too!!! )

    so
    ( 100 + 1 ) * 100 / 2 = 5050
    just said what has been said before with more detail

    PS: if you wonder why we use (n) for count, not just simply (l)
    this helps with one of two cases (or maye more)
    you dont start with 1, ie. sum from 50 to 100, then use
    a=50
    l=100
    n=51 (be aware here)
    of even numbers from 0 to 100
    a=0
    l=100
    n=51

  9. #9
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    use the formula

    sum = n(n+1)/2

    instead of looping
    Regards

  10. #10

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96
    neither of those worked...
    you'll feel better when you cannot feel at all ...

  11. #11
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    The sum of an AP up to n terms is:

    S = n(2a + (n-1)d)/2

    a is the first term, and d is the common difference.

    For the sequence 1,2,3,4...99,100:

    a = d = 1

    So S = 100(2 + (99)1)/2 = 50 * 101 = 5050

  12. #12
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    TheManWhoCan whats the difference between your and mine formula :

    S = n(2a + (n-1)d)/2


    here a = d = 1

    So S = n(2 + n-1)/2
    or
    S = n(n+1)/2

    isn't this my formula (infact this is a very well known formula of maths)

    Anyway urs is a generic one
    Regards

  13. #13
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    There is no difference between them for this case, I just thought I'd show a general method so that if the thread starter decided he wanted to sum 1,3,5,7,9...,99 he could do so.

  14. #14
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    Thats kind of you.
    Regards

  15. #15
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    I'm a kind person

  16. #16

    Thread Starter
    Lively Member heartisablack's Avatar
    Join Date
    Nov 2003
    Location
    new jersey
    Posts
    96
    hey could you help me out?

    how could i get this:

    Private Sub cmdPattern8_Click()
    lstDisplay.Clear
    Dim a As Single
    Dim b As Single
    Dim c As Single
    Dim i As Single

    a = 0
    b = 1
    intorder = 1
    For i = 0 To 24
    c = a + b
    lstDisplay.AddItem intorder & ") " & c
    intorder = intorder + 1
    b = a
    a = c
    Next i
    End Sub

    to start at 0, rather 1 for the first number?
    thanks.
    you'll feel better when you cannot feel at all ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width