Wednesday, March 14, 2018

Happy Pi Day

Code:

from mosek.fusion import *
from math import sqrt

def pi(n):
  M = Model()
  x = M.variable(n, Domain.inQCone())
  M.constraint(Expr.mulElm( range(1,n), x.slice(1,n) ),
               Domain.greaterThan(sqrt(6)))
  M.objective(ObjectiveSense.Minimize, x.index(0))
  M.solve()
  return M.primalObjValue()

print pi(100)
print pi(1000)
print pi(10000)
print pi(100000)
print pi(200000)


Output:

3.13198074852
3.14063710035
3.14149715507
3.14158310484
3.14158824714


Hint: 

$\sum_{i=1}^\infty\frac{1}{i^2}=\frac{\pi^2}{6}$.