473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Factorial and Gamma Functions for VB

kadghar
1,295 Expert 1GB
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type.

Why is this?

You can use factorial on integers (that should be the input), the output could be a string or a double, anyway you will alwas have the factorial. The only restriction is in the input.

Yes, i mean this function works fine for values up to 170! or something like that:

Expand|Select|Wrap|Line Numbers
  1. Public Function Factorial(ByVal bNum As Integer) As Double
  2.   Dim I As Double
  3.   If bNum <= 0 Then Exit Function
  4.   Factorial = 1
  5.   For I = 1 To bNum
  6.     Factorial = Factorial * I
  7.   Next
  8. End Function
Also VB can be used for making a little bit more complex functions like a Gamma.

What is a Gamma?

A Gamma Function is the generalization of the Factorial Function (this means you can use any positive number in it). If you use a Gamma for an integer, it'll give you the Factorial of the previus integer. i.e.:

G(5) = 4 ! = 24

The advantage is that even if you cannot have 4.25 ! , you can always have
G(5.25) = 35.2088612458766

Cool!!!

To obtain it, you can use many numerical methods. Here I used an old friend by Euler-Mascheroni.

Expand|Select|Wrap|Line Numbers
  1. Public Function Gamma(ByVal Dou1 As Double) As Double
  2.     Dim I As Integer
  3.     Dim Dou2 As Double
  4.     Dim Dou3 As Double
  5.  
  6.     If Fix(Dou1) = Dou1 Then
  7.         If Dou1 = 0 Or Dou1 = 1 Then
  8.             Gamma = 1
  9.             Exit Function
  10.         Else
  11.             Dou2 = 1
  12.             For I = 1 To Dou1 - 1
  13.                 Dou2 = Dou2 * I
  14.             Next
  15.             Gamma = Dou2
  16.             Exit Function
  17.         End If
  18.     ElseIf Dou1 > 3 Then
  19.         Dou2 = 1
  20.         For I = 1 To (Fix(Dou1) - 1)
  21.             Dou2 = Dou2 * (Dou1 - 1)
  22.             Dou1 = Dou1 - 1
  23.         Next
  24.         Dou3 = Exp(-0.57721566 * Dou1) / Dou1
  25.         For I = 1 To 9999
  26.             Dou3 = Dou3 * ((1 + (Dou1 / I)) ^ (-1)) * Exp(Dou1 / I)
  27.         Next
  28.         Dou3 = Dou2 * Dou3
  29.         Gamma = Dou3
  30.     Else
  31.         Dou3 = Exp(-0.577215664901533 * Dou1) / Dou1
  32.         For I = 1 To 9999
  33.             Dou3 = Dou3 * ((1 + (Dou1 / I)) ^ (-1)) * Exp(Dou1 / I)
  34.         Next
  35.         Gamma = Dou3
  36.     End If
  37. End Function
I hope this could be of some use next time you're dealing with someones homework.

Kad
Jun 13 '07 #1
0 12229

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such a thing called decorators ? tia KM
2
by: Scott | last post by:
MySQL calls 4.1.5 a "Gamma" release. What does that mean?
13
by: Eduardo78 | last post by:
Ok, this is what i am tying to do: I want to create a rutine that takes a value from a text box. Lets say for example 5. and does the following 5*4*3*2*1 = 120 the rutine should be able to...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
58
by: mailursubbu | last post by:
Hi How to write a program to get the factorial of 4096. I am working on a Linux m/c. Best Regards, Subra
8
by: salman | last post by:
this program is giving compile time error. so plse ge me the logic of factorial # include <iostream.h> # include <math.h> void main() { int f,sum=0,i,j,n; cout<<"\nEnter Number: ";
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.