|
-
May 18th, 2001, 05:40 PM
#1
......n!
How do I use n! in VB? (eg 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720)
Cheers!
-
May 18th, 2001, 05:48 PM
#2
transcendental analytic
manual multiplication 
Code:
r=1
For x=2 to n
r=r*x
next x
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 18th, 2001, 05:52 PM
#3
Banned
I think that a For ... Next block in a function is the only way.
Code:
Public Function Factorial(n as integer) 'the data type you want
factorial=1
for a=2 to n
factorial=factorial*a
next a
End Function
If there's an easier way, I would like to see it too.
Last edited by Good Dreams; May 18th, 2001 at 05:55 PM.
-
May 18th, 2001, 05:58 PM
#4
Originally posted by Good Dreams
If there's an easier way, I would like to see it too.
yep there is, just sorted it
Function Factorial(N As Integer)
Factorial = 1
For X = 2 To N
Factorial = Factorial * X
Next X
End Function
Thanks!
Note: 500th post!!!!!
-
May 18th, 2001, 05:59 PM
#5
transcendental analytic
Note: Simone learns 
btw the inline solution is more efficient
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 18th, 2001, 06:00 PM
#6
Banned
Originally posted by chenko
Note: 500th post!!!!!
-
May 18th, 2001, 06:14 PM
#7
Lively Member
defined recursively its like this:
Code:
function Fact (x) as long
if x=0 then
Fact = 1
else
Fact = x * Fact(x-1)
end if
end function
-
May 18th, 2001, 08:44 PM
#8
Frenzied Member
The dynamic programming algorithm is much more efficient in this case though, could save you a lot of function calls.
The only problem with using a long datatype is that it will only go up to 12! before it overflows. I'd suggest a double datatype, if you can sacrifice a little accuracy. If you need the range as well as the accuracy, then you could use a string implementation of a number of unlimited accuracy (well unless you run out of memory).
Harry.
"From one thing, know ten thousand things."
-
May 19th, 2001, 10:51 AM
#9
Originally posted by kedaman
Note: Simone learns
A whole load of notes you have there
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
|