# Calculate the Gamma function for half-integer arguments

Gamma function is defined as

It is a well-known fact that for positive integers it coincides with a properly shifted factorial function: Γ(n) = (n - 1)!. However, a less famous fact is

Γ(1/2) = π1/2

Actually, the Gamma function can be evaluated for all half-integer arguments, and the result is a rational number multiplied by π1/2. The formulas, as mentioned in Wikipedia, are:

(for n > 0)

In this challenge, you should write code (program or function) that receives an integer n and returns the rational representation of Γ(1/2 + n) / π1/2, as a rational-number object, a pair of integers or a visual plain-text representation like 15/8.

The numerator and denominator don't need to be relatively prime. The code should support values of n at least in the range -10 ... 10 (I chose 10 because 20! < 264 and 19!! < 232).

Test cases (taken from Wikipedia):

Input | Output (visual) | Output (pair of numbers)
0     | 1               | 1, 1
1     | 1/2             | 1, 2
-1    | -2              | -2, 1
-2    | 4/3             | 4, 3
-3    | -8/15           | -8, 15

• What kind of built-ins are allowed/forbidden? Gamma function? Pi function? – Dennis May 3 '16 at 22:21
• All built-ins are allowed (it's the default, isn't it?): gamma-function, factorial, whatever. – anatolyg May 3 '16 at 22:23
• Can I use the letter r instead of /? – Conor O'Brien May 3 '16 at 23:15
• Yes (just because I want to know how that would give you an advantage). – anatolyg May 3 '16 at 23:24
• J uses r (rational) as fraction separator. – Dennis May 3 '16 at 23:43

# M, 7 bytes

,0_.!÷/


### Trivia

Meet M!

M is a fork of Jelly, aimed at mathematical challenges. The core difference between Jelly and M is that M uses infinite precision for all internal calculations, representing results symbolically. Once M is more mature, Jelly will gradually become more multi-purpose and less math-oriented.

M is very much work in progress (full of bugs, and not really that different from Jelly right now), but it works like a charm for this challenge and I just couldn't resist.

### How it works

,0_.!÷/  Main link. Argument: n

,0       Pair with 0. Yields [n, 0].
_.     Subtract 1/2. Yields [n - 1/2, -1/2].
!    Apply the Π function. Yields [Π(n - 1/2), Π(-1/2)] = [Γ(n + 1/2), Γ(1/2)].
÷/  Reduce by division. Yields Γ(n + 1/2) ÷ Γ(1/2) = Γ(n + 1/2) ÷ √π.


# Octave, 27 bytes

@(n)rats(gamma(n+.5)/pi^.5)


Built-ins all the way.

Test suite on ideone.

• If you have rats I think you'll need a cat. – flawr May 4 '16 at 11:05

# Pyth - 272625 24 bytes

Too cumbersome for my liking. Also, implicit input seems not to work with W. Uses the second to last first form listed above.

_W<QZ,.!yK.aQ*^*4._QK.!K


# J, 201719 18 bytes

Saved 3 bytes using @Dennis's technique in his M, and another! This is a tacit verb.

x:@%&!/@(0.5-~,&0)


(Previous answers: x:@%/@(!@-&0.5@,&0), x:%/(!@-&0.5@,&0), x:((%:o.1)%~!@-&0.5))

(Note that instead of being -a/b, it's _arb.)

Test cases:

   gamma =: x:@%&!/@(0.5-~,&0)
gmR0  =: gamma"0    NB. apply to 0-cells of a list
gamma 0
1
gamma 1
1r2
gamma 2
3r4
gamma -1
_2
gamma -2
4r3
i:2
_2 _1 0 1 2
gmR0 i:2
4r3 _2 1 1r2 3r4
gmR0 i:5
_32r945 16r105 _8r15 4r3 _2 1 1r2 3r4 15r8 105r16 945r32


# Mathematica, 17 15 bytes

(#-1/2)!/√π&


Uses factorial function since gamma(n + 1/2) = (n - 1/2)!.

There are only 12 characters now but it needs 15 bytes to be represented. The bytes are counted using UTF-8. Sqrt = √ is 3 bytes from 5 bytes with @ (2 bytes saved, thanks to @CatsAreFluffy) and Pi = π is 2 bytes (equal).

import Data.Ratio
p=product
f n|n<0=1/(p[n+1%2..0-1])|0<1=p[1%2..n-1]


This seems bloated, but I'm not sure where to golf this from here. Any golfing suggestions are welcome.

Edit: Used the fact that the default step of range [x..y] is 1, and moved product to p=product to save two bytes.

Ungolfing:

import Data.Ratio
gamma_half n | n < 0     = 1 / ( product [ n + 1%2 .. (-1) ] )
| otherwise = product [ 1%2 .. n-1 ]


# Ruby, 8059 58 bytes

f=->n{n<0?(-1)**n/f[-n]:(1..n).reduce(1){|z,a|z*(a-1r/2)}}


Any golfing suggestions are welcome.

Ungolfing:

def half_gamma(n)
if n < 0
return (-1) ** n / f(-n)
else
# Is there an golfier way to write product([1/2,3/2..(2*n-1)/2])?
return (1..n).reduce(1) {|z, a| z * (a - 1r/2)}
end
end