Results 1 to 9 of 9

Thread: ......n!

  1. #1
    chenko
    Guest

    Unhappy ......n!

    How do I use n! in VB? (eg 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720)


    Cheers!

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Banned
    Join Date
    Feb 2001
    Location
    Back to sh*tland
    Posts
    294
    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.

  4. #4
    chenko
    Guest
    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!!!!!

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Banned
    Join Date
    Feb 2001
    Location
    Back to sh*tland
    Posts
    294
    Originally posted by chenko

    Note: 500th post!!!!!

  7. #7
    Lively Member
    Join Date
    May 2000
    Posts
    84
    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

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  9. #9
    chenko
    Guest
    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
  •  



Click Here to Expand Forum to Full Width