Click to See Complete Forum and Search --> : 1 to 100 ...
heartisablack
Nov 25th, 2003, 10:51 PM
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
DiGiTaIErRoR
Nov 26th, 2003, 12:45 AM
Sum of 1 to 100?
You mean 100? Or.. 1+2+3.. etc to 100.
(ubound-lbound)+1 = range(count)
heartisablack
Nov 26th, 2003, 12:49 AM
i mean the second option you gave, 1+2+3 ...ect to 100 ...
what is wrong with my coding?
sql_lall
Nov 26th, 2003, 02:04 AM
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:
intw = 0
For intp = 0 to 100
intw = intw + intp '*****
Next
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 *****
heartisablack
Nov 26th, 2003, 02:33 AM
so it should be 5050?... thanks so much.
wossname
Nov 27th, 2003, 07:43 AM
101 * 50 = 5050
heartisablack
Nov 27th, 2003, 08:17 PM
haha so it is right?
ZaidGS
Nov 29th, 2003, 07:36 AM
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
sw_is_great
Nov 29th, 2003, 03:25 PM
use the formula
sum = n(n+1)/2
instead of looping
heartisablack
Nov 30th, 2003, 02:05 AM
neither of those worked...
TheManWhoCan
Nov 30th, 2003, 05:33 AM
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
sw_is_great
Nov 30th, 2003, 06:11 AM
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
TheManWhoCan
Nov 30th, 2003, 02:04 PM
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.
sw_is_great
Dec 1st, 2003, 04:29 AM
Thats kind of you.
:D
TheManWhoCan
Dec 1st, 2003, 11:32 AM
I'm a kind person:D :D
heartisablack
Dec 2nd, 2003, 12:35 AM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.