how to exit recursive math formula and still get an answer

Posted by calccrypto on Stack Overflow See other posts from Stack Overflow or by calccrypto
Published on 2010-04-30T20:15:18Z Indexed on 2010/04/30 20:17 UTC
Read the original article Hit count: 172

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about python

Related posts about math