473,390 Members | 1,265 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,390 developers and data experts.

Factorial and Gamma Functions for VB

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


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
  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.

Jun 13 '07 #1
0 12227

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

Similar topics

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
by: Scott | last post by:
MySQL calls 4.1.5 a "Gamma" release. What does that mean?
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...
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...
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;
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
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: ";
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...
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...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...

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.