Jul 20, 2008

Factorial

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Factorial

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

CODE
<?php
class 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!

Reply

Erdemir
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

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : factorial


    Looking for factorial

Searching Video's for factorial
advertisement



Factorial



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE