xisto Community

# Factorial

## Recommended Posts

Here is my 2nd Code:
By this class you can calculate FACTORIAL of a number:

<?phpclass factorial_math {function factorial(\$num) {if ((\$num == 0)||(\$num == 1)) { return 1; } else {return \$num*\$this->factorial(\$num-1);} }function calculating_e() {\$iterations = 15;\$fact = 1;for (\$n=1; \$n<=\$iterations; \$n++) {\$fact = \$fact*\$n;\$chan = 1/\$fact;\$result = \$result + \$chan; }return \$result+1; } }// example\$fuct = new factorial();print(\$fuct->factorial(10));?>

Enjoy!

##### Share on other sites

This function is only working with natural numbers (0..infinity)

For this range your function is working fine.

But this could not calculate all the real numbers like -6, -8.323, 0.5 etc.

If you go to Mathematical Department in university or Mechanical Engineering or Electric-Electronic Engineering in the lesson Differential Equations or Laplace, you must learn that we have learnt the factorial function wrong before.

Until high school the teachers teach us wrong that factorial works with only 0,1,2...infinity.

Actually factorial works with all real numbers.

For example:

factorial(2) = 2

factorial(4) = 24

factorial(5) = 120

and also

factorial(1/2)=factorial(0.5)= sqrt(pi) =~ 1,77245385

If you don't believe me go to a mathematic professor in a university and ask him, factorial(1/2)?

By the way the formula of factorial is

n! = integral[0 to infinity] (t^(n-1)) * e^(-t) * dt

I couldn't write the integral symbol so I wrote: integral[0 to infinity]

I found a picture from wikipedia

//Edit reason: the formula inserted

Edited by Erdemir (see edit history)

## Create an account

Register a new account

×

• #### Activity

×
• Create New...