Posted By

indra on 10/26/10


Tagged

algorithm


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Calculate Factorial


 / Published in: VB.NET
 

URL: http://www.daniweb.com/forums/thread10589.html

  1. //To calculate a factorial the simplest way and more optimized, you may use the following function
  2.  
  3. Private Function Factorial(ByVal Num As Int64) As Int64
  4. Factorial = 1
  5. For i = 2 To Num
  6. Factorial *= Num
  7. Next
  8. End Function
  9.  
  10. //Please be aware that there is an inherent max to all factorial calculations. Based on x86 or x64.
  11. //Let alone the data types being supported. I used a Int64 instead of Integer because Integer is
  12. //actually a wrapper for Int32, which has a smaller min/max
  13.  
  14. //In some cases, you may want to use:
  15.  
  16. Private Function Factorial(ByVal Num As UInt64) As UInt64
  17. Dim i As UInt64
  18. Factorial = 1
  19. For i = 2 To Num
  20. Factorial *= Num
  21. Next
  22. End Function
  23.  
  24. //seeing as the UInt64 allows for a MUCH bigger maximum than the Int64 does. U stands for unsigned,
  25. //meaning its positive numbers only.

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: housecor on October 26, 2010

Mistagged. This is VB.Net, not C#.

You need to login to post a comment.