advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
Tip of the Day
Rate this item | 0 users have rated this item.
Tip formerly from VB2TheMax
Expertise: beginner
Language: VB4,VB5,VB6,VBS
May 27, 2000
Factorial - The factorial of a number
' The factorial of a number
'
' if NUMBER is negative or >170 it raises an
' "subscript out of range" error

Function Factorial(ByVal number As Long) As Double
    Static result(170) As Double
    
    ' this routine is very fast because it
    ' caches all the possible results
    If result(0) = 0 Then
        ' this is the first time it is executed
        Dim i As Long
        result(0) = 1
        ' Factorial(170) is the highest factorial
        ' value that can be stored in a Double
        For i = 1 To 170
            result(i) = result(i - 1) * i
        Next
    End If
    
    ' just read the result from the cached array
    Factorial = result(number)
        
End Function


' *** UPDATE ***
' Rick Rothstein (MVP-VB) sent us the following update for the Factorial 
' function. It reports whole number results up to an argument value of 27 (a 29-
' digit answer containing 23 significant digits) before switching to power-of-
' ten notation.


' The factorial of a number
'
' if NUMBER is negative or >170 it raises an
' "subscript out of range" error
Function Factorial(ByVal N As Integer) As Variant
    Static Result(170) As Variant
    Dim X As Integer
    ' this routine is very fast because it
    ' caches all the possible results
    If Result(0) = 0 Then
        ' convert to Decimal data type
        Result(0) = CDec(1)
        ' Factorial(170) is the highest factorial
        ' value that can be stored in a Double
        For X = 1 To 170
            ' for X<28, calculate using Decimal data
            ' type; use Double data type afterwards
            If X = 28 Then
                Result(28) = 28 * CDbl(Result(27))
            Else
                Result(X) = X * Result(X - 1)
            End If
        Next
    End If
    ' just read the result from the cached array
    Factorial = Result(N)
End Function
Francesco Balena
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES