Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2010-04-29 12:28:46

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Getting an answer despite exiting recursion

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:

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

 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


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

Board footer

Powered by FluxBB