ガンマ関数

############################################
# ガンマ関数の計算
#      coded by Y.Suganuma
############################################

############################################
# Γ(x)の計算(ガンマ関数,近似式)
#      ier : =0 : normal
#            =-1 : x=-n (n=0,1,2,・・・)
#      return : 結果
#      coded by Y.Suganuma
############################################

def gamma(x, ier)

	ier[0] = 0

	if x > 5.0
		v = 1.0 / x
		s = ((((((-0.000592166437354 * v + 0.0000697281375837) * v + 0.00078403922172) * v - 0.000229472093621) * v - 0.00268132716049) * v + 0.00347222222222) * v + 0.0833333333333) * v + 1.0
		g = 2.506628274631001 * Math.exp(-x) * (x ** (x-0.5)) * s

	else

		err = 1.0e-20
		w   = x
		t   = 1.0

		if x < 1.5

			if x < err
				k = Integer(x)
				y = Float(k) - x
				if y.abs() < err or (1.0-y).abs() < err
					ier[0] = -1
				end
			end

			if ier[0] == 0
				while w < 1.5
					t /= w
					w += 1.0
				end
			end

		else
			if w > 2.5
				while w > 2.5
					w -= 1.0
					t *= w
				end
			end
		end

		w -= 2.0
		g  = (((((((0.0021385778 * w - 0.0034961289) * w + 0.0122995771) * w - 0.00012513767) * w + 0.0740648982) * w + 0.0815652323) * w + 0.411849671) * w + 0.422784604) * w + 0.999999926
		g *= t
	end

	return g
end

ind = Array.new(1)
print("data? ")
s = gets()
x = Float(s)

y = gamma(x, ind)

print("   x " + String(x) + " gamma(x) " + String(y) + " ind " + String(ind[0]) + "\n")