Posted By

indra on 10/26/10



Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Calculate Factorial

 / Published in: VB.NET


  1. //To calculate a factorial the simplest way and more optimized, you may use the following function
  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
  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
  14. //In some cases, you may want to use:
  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
  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  


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.