7
\$\begingroup\$

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

7 Answers 7

7
\$\begingroup\$

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) ÷ √π.
\$\endgroup\$
0
5
\$\begingroup\$

Octave, 27 bytes

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

Built-ins all the way.

Test suite on ideone.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you have rats I think you'll need a cat. \$\endgroup\$
    – flawr
    May 4, 2016 at 11:05
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
0
2
\$\begingroup\$

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

\$\endgroup\$
0
1
\$\begingroup\$

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 ]
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.