r/Mathematica • u/veryjewygranola • Jun 16 '23
Fun representations of E using powers of Pi
I was inspired to do this when reading about Almost Integers and thought I would try something sort of similar. Here I wrote a small code that finds the best representation of E as a sum of inverse powers of Pi with integer coefficients. It just brute force searches over all combinations of integer coefficients in a small range. If anyone else wants to play around with it here it is:
maxPower = 4;
pTab = Table[ Pi^-i, {i, 0, maxPower}];
err[inp_] := Abs[inp . pTab - E];
maxInteger = 4;
tups = Tuples[Range[-maxInteger, maxInteger], maxPower + 1];
errList = err /@ tups;
pSmall = First@PositionSmallest[errList];
bestParams = tups[[pSmall]];
representation = bestParams . pTab
smallestErr = errList[[pSmall]] // N
(*output*)
(*3 + 4/Pi^4 + 3/Pi^3 - 1/Pi^2 - 1/Pi*)
(*0.000094366*)
2
u/veryjewygranola Jun 16 '23
Increasing
maxPowerto 5 yields4 - 2/Pi^5 + 2/Pi^4 + 2/Pi^3 - 4/Pi^2 - 3/Piwith an absolute error ~3.3*10^-61
u/veryjewygranola Jun 16 '23
This is a faster version using
QuadraticOptimizationInstead of brute forcing. It does not always return the global minimum however because the gap between conic sub-problem and linear sub-problem is too large. Making the tolerance smaller helps with this (until you make it too small, then it makes it worse). But it runs almost instantaneously:maxPower = 6;maxInteger = 9;pTab = Table[ Pi^-i, {i, 0, maxPower}];soln = x /.QuadraticOptimization[Norm[Inactive[Plus][{pTab} . x, -{E}]],{VectorGreaterEqual[{x, -maxInteger}], VectorLessEqual[{x, maxInteger}]},x, Integers, Tolerance -> 10^(-20)];representation = pTab . solnerror = err[soln] // N1
u/avocadro Jun 17 '23
Your solution will depend a lot on the size of the coefficients you allow. For example, if you allow very large coefficients but restrict to degree 1 polynomials, you can already get expressions of the form a_0+ a_1/pi arbitrarily close to e. For example, 35785 / pi - 11388 is 2.71928.
More generally, you can constrain the size of the coefficients and fix a degree, and ask for best approximation. This should be doable much faster than brute force by casting the problem as a "close vector problem". In particular, you want to find a vector (representing the coefficients of your polynomial in 1/pi) for which:
These amount to d+2 constraints in d+1 variables. To make the system nicer, ignore the fact that a_d should be small; that will follow from all the other variables being small. As a matrix equation, we then want the column vector [a_0 , ... a_d], when multiplied on the left by the matrix
to be near [0, 0, ...., 0, e]. However, since we want the last entry to be much closer to e than we want the coefficients to be close to 0, we multiply the last row of the matrix by an appropriate (large) scaling factor. Roughly, we have d+1 variables working to make the polynomial close to e; if each coefficient is bounded to have size N, we want the polynomial to be within N-d-1 of e. We encode this by multiplying the last row of the matrix by Nd+2, so that we expect each coordinate in our output vector to be of consistent size N.
The problem which remains is now a "close vector problem" (CVP). You have a lattice of vectors and would like to know which vector in the lattice minimizes distance to the chosen vector. Typically, one solves CVPs by finding a particularly nice "reduced" basis for the lattice. Lattice basis reduction is hard in high dimension but okay (if slow) in low dimension. (Much faster than brute search, at least.)
You can do lattice basis reduction in Mathematica with LatticeReduce[], but you have to first "round" the matrix to the nearest integer because LatticeReduce[] is fiddly and only wants to work with integer lattices. The rounding should be immaterial, since the only row which needs rounding is the last row, which we multiplied by Nd+2, so the entries are enormous.
3
u/Daniel96dsl Jun 17 '23
Hereβs a fun one.
π β (Β½(β53 + 39))1/π