Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The Pi function is an extension of the factorial over the reals (or even complex numbers). For integers n, Π(n) = n!, but to get a definition over the reals we define it using an integral:

Pi(z) = integral t from 0 to infinity e^-t t^z dt

In this challenge we will invert the Π function.

Given a real number z ≥ 1, find positive x such that Π(x) = z. Your answer must be accurate for at least 5 decimal digits.


Examples:

120 -> 5.0000
10 -> 3.39008
3.14 -> 2.44815
2017 -> 6.53847
1.5 -> 1.66277
share|improve this question
2  
Note that more often people use the Gamma (Γ) function. Π(x) = Γ(x+1). But IMO Γ is a shifted abomination, and Π is the true extension of the factorial. – orlp 12 hours ago
    
Wellp, that series expansion is enough to scare me... i.imgur.com/ttgzDSJ.gif – carusocomputing 12 hours ago
1  
@LuisMendo You may assume z ≥ 1, I edited the question. – orlp 12 hours ago
1  
All of the examples you give have other solutions as well, for example 120 -> -0.991706. This is because Π(x) goes to infinity as x goes to -1 from the right. Perhaps you mean to insist that x>0 as well. – Greg Martin 12 hours ago
    
@GregMartin Added as well. – orlp 12 hours ago

Mathematica, 17 15 27 bytes

FindInstance[#==x!&&x>0,x]&

Output looks like {{x -> n}}, where n is the solution, which may not be allowed.

share|improve this answer

MATL, 13 bytes

1`1e-5+tQYgG<

This uses linear seach in steps of 1e-5 starting at 1. So it is terribly slow, and times out in the online compiler.

To test it, the following link replaces the 1e-5 accuracy requirement by 1e-2. Try it online!

Explanation

1        % Push 1 (initial value)
`        % Do...while
  1e-5   %   Push 1e-5
  +      %   Add
  t      %   Duplicate
  QYg    %   Pi function (increase by 1, apply gamma function)
  G<     %   Is it less than the input? If so: next iteration
         % End (implicit)
         % Display (implicit)
share|improve this answer

Pyth, 4 bytes

.I.!

A program that takes input of a number and prints the result.

Test suite

How it works

.I.!    Program. Input: Q
.I.!GQ  Implicit variable fill
.I      Find x such that:
  .!G    gamma(x+1)
     Q   == Q
        Implicitly print
share|improve this answer

MATLAB, 59 bytes

@(x)fminsearch(@(t)(gamma(t+1)-x)^2,1,optimset('TolF',eps))

This is an anonymous function that finds the minimizer of the squared difference betweeen the Pi function and its input, starting at 1, with very small tolerance (given by eps) to achieve the desired precision.

Test cases (run on Matlab R2015b):

>> @(x)fminsearch(@(t)(gamma(t+1)-x)^2,1,optimset('TolF',eps))
ans = 
    @(x)fminsearch(@(t)(gamma(t+1)-x)^2,1,optimset('TolF',eps))
>> f = ans; format long; f(120), f(10), f(3.14), f(2017)
ans =
   5.000000000000008
ans =
   3.390077650547032
ans =
   2.448151165246967
ans =
   6.538472664321318

You could try it online in Octave, but unfortunately some of the results lack the required precision.

share|improve this answer

GeoGebra, 25 bytes

NSolve[Gamma(x+1)=A1,x=1]

Entered in the CAS input, and expects input of a number in spreadsheet cell A1. Returns a one-element array of the form {x = <result>}.

Here is a gif of the execution:

Execution of progrma

How it works

NumericallySolve the following equation: Gamma(x+1)=A1, with starting value x=1.

share|improve this answer
    
Is it guaranteed to return a positive number, and does it work for 1.5, which has broken several answers? – Pavel 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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