Calculate the Gamma function for half-integer arguments

6

1

Gamma function is defined as

Gamma function definition

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:

Gamma function for half-integer arguments (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

anatolyg

Posted 2016-05-03T22:17:35.690

Reputation: 10 719

What kind of built-ins are allowed/forbidden? Gamma function? Pi function? – Dennis – 2016-05-03T22:21:32.677

All built-ins are allowed (it's the default, isn't it?): gamma-function, factorial, whatever. – anatolyg – 2016-05-03T22:23:39.287

Can I use the letter r instead of /? – Conor O'Brien – 2016-05-03T23:15:11.463

Yes (just because I want to know how that would give you an advantage). – anatolyg – 2016-05-03T23:24:30.910

J uses r (rational) as fraction separator. – Dennis – 2016-05-03T23:43:01.053

This is a subset of http://codegolf.stackexchange.com/questions/63887/gamma-function-golf

– Mego – 2016-05-04T04:18:17.557

@Mego Except that this requires exact rational output. – user202729 – 2018-03-04T10:28:07.247

@user202729 Hence why I just commented (nearly a year ago), rather than closing it as a dupe – Mego – 2018-03-04T15:42:15.410

Answers

7

M, 7 bytes

,0_.!÷/

Try it online! or verify all test cases.

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) ÷ √π.

Dennis

Posted 2016-05-03T22:17:35.690

Reputation: 196 637

5

Octave, 27 bytes

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

Built-ins all the way.

Test suite on ideone.

beaker

Posted 2016-05-03T22:17:35.690

Reputation: 2 349

1If you have rats I think you'll need a cat. – flawr – 2016-05-04T11:05:30.683

3

Pyth - 27 26 25 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

Test Suite.

Maltysen

Posted 2016-05-03T22:17:35.690

Reputation: 25 023

2

J, 20 17 19 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

Conor O'Brien

Posted 2016-05-03T22:17:35.690

Reputation: 36 228

2

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).

Example

miles

Posted 2016-05-03T22:17:35.690

Reputation: 15 654

1

Haskell, 81 71 69 bytes

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 ]

Sherlock9

Posted 2016-05-03T22:17:35.690

Reputation: 11 664

0

Ruby, 80 59 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

Sherlock9

Posted 2016-05-03T22:17:35.690

Reputation: 11 664