tags:

views:

115

answers:

2

i wrote this python code, which from wolfram alpha says that its supposed to return the factorial of any positive value (i probably messed up somewhere), integer or not:

from math import *

def double_factorial(n):
    if int(n) == n:
        n = int(n)
        if [0,1].__contains__(n):
            return 1
        a = (n&1) + 2
        b = 1
        while a<=n:
            b*=a
            a+= 2
        return float(b)
    else:
        return factorials(n/2) * 2**(n/2) *(pi/2)**(.25 *(-1+cos(n * pi)))

def factorials(n):
    return pi**(.5 * sin(n*pi)**2) * 2**(-n + .25 * (-1 + cos(2*n*pi))) * double_factorial(2*n)

the problem is , say i input pi to 6 decimal places. 2*n will not become a float with 0 as its decimals any time soon, so the equation turns out to be

pi**(.5 * sin(n*pi)**2) * 2**(-n + .25 * (-1 + cos(2*n*pi))) * double_factorial(loop(loop(loop(...)))))

how would i stop the recursion and still get the answer?

ive had suggestions to add an index to the definitions or something, but the problem is, if the code stops when it reaches an index, there is still no answer to put back into the previous "nests" or whatever you call them

A: 

In your code, you define double_factorial like

double_factorial(n) = factorial(n/2) * f(n)      ... (1)

and in the factorial you define it as

factorial(n) = double_factorial(2*n) / f(2*n)    ... (2)

which is equivalent to equation (1), so you created a circular reference without an exit point. Even math can't help. You have to define either factorial or double_factorial, e.g.

def factorials(n):
    return tgamma(n + 1)
KennyTM
i figured as much... so how does wolfram alpha do it?
calccrypto
@calccrypto: Wolfram Alpha is based on Mathematica which can solve equations symbolically. You can't use techniques for Mathematica in Python directly.
KennyTM
+1  A: 

You defined f in terms of g and g in terms of f. But you don't just have a circular definition with no base point to start the recursion. You have something worse. The definition of f is actually the definition of g inverted. f is precisely undoing what g did and vice versa. If you're trying to implement gamma yourself (ie. not using the one that's already there in the libraries) then you need to use a formula that expresses gamma in terms of something else that you know how to evaluate. Just using one formula and its inversion like that is a method that will fail for almost any problem you apply it to.

im not trying to do gamma. double factorial is: input n. multiply every other value down to 2 or 3, depending on if the value is even or odd
calccrypto
If you're trying to compute double factorial for arbitrary real numbers you're effectively computing gamma. (http://en.wikipedia.org/wiki/Gamma_function#Properties) What arguments are you expecting to pass in? Any float value? That doesn't fit your description of multiplying every other value because even or odd doesn't apply to real numbers that aren't integers.If you want to use that formula I gave in terms of gamma, and want to implement gamma yourself, there's code in the source for libm.