Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

Post a reply

Go back

Write your message and submit
:) :| :( :D :o ;) :/ :P :lol: :mad: :rolleyes: :cool: | :dizzy :eek :kiss :roflol :rolleyes :shame :down :up :touched :sleep :wave :swear :tongue :what :faint :dunno
Options

Go back

Topic review (newest first)

calccrypto
2010-04-30 10:28:46

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

Code:

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                          # checks if its even or odd and starts the a value at 2 or 3
        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)))                  # go to the next definition

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

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

Code:

 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

Board footer

Powered by FluxBB