0 Members and 2 Guests are viewing this topic.
Maybe I should have rephrased a bit differently: The gamma function for positive integers follows the following property: gamma(x)=(x-1)!. Hence gamma(1)=0!=1, for instance.However, what is gamma(0)? It would equal to (-1)!. But using the factorial recurrence formula would give us 0(-1)!=0!=1. In other words, (-1)! = 1/0. This is bad; hence, (-1)! is not defined, and so is gamma(0).Most other numbers still have a gamma function attached to it, for example gamma(-1/2), gamma(3+4i), and gamma(1337.42i), but zero and negative numbers when plugged into gamma generate an undefined result, because their respective factorials are undefined as well.EDIT: Aw darn, sniped
def getFactorial(x): if x == 0: return 1 else: soma = 1 for i in range(x,0,-1): soma=soma*i return soma
import mathprint math.factorial(x)
public int factorial(int x){ if(x == 0) return 1; return x * factorial(x - 1);}
public int factorial(int x){ int sum = x == 0 ? 1 : x; for(int i = x; --i > 0;) sum *= i; return sum;}
double sum=0;for (int i=1; i<Max; i++) { sum += log(i);}int exponent = floor(sum);double coeff = Pow(10,sum-exponent);print("Answer is: ",coeff,"x10^",exponent);
Code: [Select]def getFactorial(x): if x == 0: return 1 else: soma = 1 for i in range(x,0,-1): soma=soma*i return somaI just made a Python function that gets the factorial of a n number.A simpler way of doing this is:Code: [Select]import mathprint math.factorial(x)But mine is much cooler.
Yet another cool way to compute factorials, this way is for factorials of very very large numbers without multi-precision containers.No multiplication required Code: (Pseudocode) [Select]double sum=0;for (int i=1; i<Max; i++) { sum += log(i);}int exponent = floor(sum);double coeff = Pow(10,sum-exponent);print("Answer is: ",coeff,"x10^",exponent);
def getFactorial(x): if x == 0: return 1 else: sum = 1 for i in range(x,0,-1): sum =sum *i return sum
def calcularFactorial(x): if x == 0: return 1 else: soma = 1 for i in range(x,0,-1): soma = soma *i return soma
Oh, you didn't need to correct the variables names. It was just an future advice. Sometimes I also end mixing different conventions.I speak Portuguese. See your personal messages.