Calculate Factorial


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your 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.

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.