|
-
Nov 25th, 2003, 11:51 PM
#1
Thread Starter
Lively Member
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 ...
-
Nov 26th, 2003, 01:45 AM
#2
So Unbanned
Re: 1 to 100 ...
Sum of 1 to 100?
You mean 100? Or.. 1+2+3.. etc to 100.
(ubound-lbound)+1 = range(count)
-
Nov 26th, 2003, 01:49 AM
#3
Thread Starter
Lively Member
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 ...
-
Nov 26th, 2003, 03:04 AM
#4
Fanatic Member
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:
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 *****
sql_lall 
-
Nov 26th, 2003, 03:33 AM
#5
Thread Starter
Lively Member
so it should be 5050?... thanks so much.
you'll feel better when you cannot feel at all ...
-
Nov 27th, 2003, 08:43 AM
#6
I don't live here any more.
-
Nov 27th, 2003, 09:17 PM
#7
Thread Starter
Lively Member
you'll feel better when you cannot feel at all ...
-
Nov 29th, 2003, 08:36 AM
#8
Addicted Member
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
-
Nov 29th, 2003, 04:25 PM
#9
Hyperactive Member
use the formula
sum = n(n+1)/2
instead of looping
-
Nov 30th, 2003, 03:05 AM
#10
Thread Starter
Lively Member
neither of those worked...
you'll feel better when you cannot feel at all ...
-
Nov 30th, 2003, 06:33 AM
#11
Lively Member
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
-
Nov 30th, 2003, 07:11 AM
#12
Hyperactive Member
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
-
Nov 30th, 2003, 03:04 PM
#13
Lively Member
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.
-
Dec 1st, 2003, 05:29 AM
#14
Hyperactive Member
Thats kind of you.
-
Dec 1st, 2003, 12:32 PM
#15
Lively Member
I'm a kind person
-
Dec 2nd, 2003, 01:35 AM
#16
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|