[Question]
  [
Inspired by [this video of *Infinite Series*](https://www.youtube.com/watch?v=ineO1tIyPfM).
### Introduction
Pi is defined as the ratio of the circumference to the diameter of a circle. But how is a circle defined? Usually a circle is defined as the points with constant distance to the centerpoint (let us assume that the center is at `(0,0)`). The next question would be: How do we define the *distance*? In the following we are considering different notions of distances (induced by the `Lp`-norms):
Given a norm (=something that measures a *length*) we can easily construct a *distance* (=distance between two points) as follows:
```
dist(A,B) := norm (A-B)
```
The euclidean norm is given by:
```
norm((x,y)) = (x^2 + y^2)^(1/2)
```
This is also called the *L2-norm*. The other *Lp-norms* are constructed by replacing the `2` in the above formula by other values between 1 and infinity:
```
norm_p((x,y)) = (|x|^p + |y|^p)^(1/p)
```
The unit circles for those different norms have quite distinct shapes:
![](https://upload.wikimedia.org/wikipedia/commons/d/d4/Vector-p-Norms_qtl1.svg)
### Challenge
Given a `p >= 1`, calculate the ratio of circumference to diameter of a *Lp-circle* with respect to the `Lp`-norm with an accuracy of four significant figures.
### Testcases
We can use that for `p,q` with `1 = 1/p + 1/q` we get the same ratio for the `Lp` as well as the `Lq` norm. Furthermore for `p = q = 2` the ratio is minimal, and for `p = 1, q = infinity` we get a ratio of 4, so the ratios are always between `pi` and `4`.
```
p   or  q            ratio
1       infinity     4
2       2            3.141592
1.623   2.60513      3.200
1.5     3            3.25976
4       1.33333      3.39693
```
  
[Answer]
# Python + scipy, 92 bytes
```
from scipy.integrate import*
lambda p:2/p*quad(lambda x:(x/x**p+(1-x)**(1-p))**(1/p),0,1)[0]
```
Formula is from [this math.SE question](https://math.stackexchange.com/questions/254620/pi-in-arbitrary-metric-spaces).
[Answer]
# [MATL](https://github.com/lmendo/MATL), 31 bytes
```
0:1e-3:1lyG^-lG/^v!d|G^!slG/^sE
```
[Try it online!](https://tio.run/nexus/matl#@29gZZiqa2xlmFPpHqeb464fV6aYUuMep1gMYhe7/v9vqGcKAA "MATL – TIO Nexus") Or [verify all test cases](https://tio.run/nexus/matl#S4jwrvhvYGWYqmtsZZhT6R2nm@OtH1emmFLjHadYDGIXu/53CflvyGXEZahnZmQMJE25TAA).
### Explanation
This generates the *x*,*y* coordinates of one quarter of the unit circle sampled at 1001 points with step 0.001 in *x*. The length of the quarter of circle is approximated by that of the polygonal line that passes through those points; that is, the sum of the lengths of the 1000 segments. Length is of course computed according to `p`-norm. Multiplying the result by 2 gives the approximate lenght of half a circle, that is, pi.
```
0:1e-3:1   % Push [0 0.001 0.002 ... 0.999 1]. These are the x coordinates of
           % the vertices of the polygonal line that will approximate a quarter
           % of the unit circle
l          % Push 1
y          % Duplicate [0 0.001 0.002 ... 0.999 1] onto the top of the stack.
G          % Push input, p
^          % Element-wise power: gives [0^p 0.001^p ... 1^p]
-          % Element-wise subtract from 1: gives [1-0^p 1-0.001^p ... 1-1^p]
lG/        % Push 1, push p, divide: gives 1/p
^          % Element-wise power: gives [(1-0^p)^(1/p) (1-0.001^p)^(1/p) ...
           % ... (1-1^p)^(1/p)]. These are the y coordinates of the vertices
           % of the polygonal line
v          % Concatenate vertically into a 2×1001 matrix. The first row contains
           % the x coordinates and the second row contains the y coordinates
!          % Transpose
d|         % Compute consecutive differences down each column. This gives a
           % 1000×2 matrix with the x and y increments of each segment. These
           % increments will be referred to as Δx, Δy
G          % Push p
^          % Element-wise power
!          % Transpose
s          % Sum of each column. This gives a 1×1000 vector containing
           % (Δx)^p+(Δy)^p for each segment
lG/        % Push 1/p
^          % Element-wise power. This gives a 1×1000 vector containing 
           % ((Δx)^p+(Δy)^p)^(1/p) for each segment, that is, the length of 
           % each segment according to p-norm
s          % Sum the lenghts of all segments. This approximates the length of
           % a quarter of the unit circle
E          % Multiply by 2. This gives the length of half unit circle, that is,
           % pi. Implicitly display
```
[Answer]
# Wolfram Language (Mathematica), ~~49~~ ~~46~~ 45 bytes
*3 bytes saved thanks to [alephalpha](https://codegolf.stackexchange.com/u/9288).*  
*1 byte saved thanks to [att](https://codegolf.stackexchange.com/u/81203).*
```
\!\(2N@∫\_0\%1\@+++(a^-#-1)^(1-#)\%#a\)&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P0YxRsPIz@FRx@qYeIMYVcMYB21tbY3EOF1lXUPNOA1DXWXNGFXl93N7EmM01f4HlmamljgEFGXmlUQr69qlOSjHqtUFJyfm1VVzGepwGelwGeqZGRmDKFMdLhOu2v8A "Wolfram Language (Mathematica) – Try It Online") Pure function. Takes a number as input and returns a number as output. The Unicode characters are U+222B for `\[Integral]` and U+F74C for `\[DifferentialD]`.
[Answer]
## PARI/GP, ~~48~~ 43 bytes
It's easy after @orlp has found the formula, and @alephalpha's version saves 5 bytes:
```
p->2*intnum(u=0,1,(1+(u^-p-1)^(1-p))^(1/p))
```
To add something slightly useful, let's calculate the `p` for which we get `3.2`:
```
? f=p->2*intnum(u=0,1,(1+(u^-p-1)^(1-p))^(1/p));
? solve(p=1,2,f(p)-3.2)
%2 = 1.623002382384469009676324702
```
### Correct usage
While the code gives results that are much more exact than the challenge demands, it can easily be improved a lot: if we replace the upper integration limit `1` with `[1,1/p-1]` (giving what the manual calls the singularity exponent) then all shown digits of `f(2)` agree with `Pi`. This is still true if we increase the precision to 100 (type `\p100`).
However, after that change the `solve` computation no longer worked. I changed the inner term to explicitely handle the case `u=0` and also changed to a different computer with a newer PARI version and 64 bit (which implies a higher default precision).
Here is the improved calculation of the `p` value for `Pi=3.2`, and let's also have a look at the real Pi:
```
? f=p->2*intnum(u=0,[1,1/p-1],if(u,(1+(u^-p-1)^(1-p))^(1/p),0));
? f(2)
%2 = 3.1415926535897932384626433832795028842
? Pi
%3 = 3.1415926535897932384626433832795028842
? solve(p=1,2,f(p)-3.2)
%4 = 1.6230023823844690096763253745604419761
```
[Answer]
# [Desmos](https://desmos.com/calculator), 42 bytes
```
f(p)=2∫_0^1(x/x^p+(1-x)^{1-p})^{1/p}dx/p
```
Port of [orlp's integration formula](https://codegolf.stackexchange.com/a/106036/68261). Yay for integration built-in!
[Try it on Desmos!](https://www.desmos.com/calculator/g5capgcp0h)
[Answer]
## JavaScript (ES7), 80 bytes
Based on [orlp's answer](https://codegolf.stackexchange.com/a/106036/58563). This JS implementation is quite slow. You may want to try `i=1e-7` (or even higher) for a faster approximation.
*Note*: This is basically intended for Chrome and Edge only. An equivalent ES6 version using `Math.pow()` on Firefox 50.1 seems to be *much* slower.
*Edit*: According to Neil, this should also work fine on Firefox 52.
```
f=
p=>{for(i=5e-8,s=x=0;(x+=i)<1;)s+=i*(x**(1-p)+(1-x)**(1-p))**(1/p);return 2/p*s}
console.log(f(1).toFixed(3))
console.log(f(2).toFixed(3))
console.log(f(1.623).toFixed(3))
```
]