TITLE: Why do Pell equations appear in Ramanujan's pi formulas?
QUESTION [50 upvotes]: While answering this MSE question about the Pell equation $x^2-29y^2=1$, I noticed that certain fundamental solutions appeared in Ramanujan's famous pi formula. 
I. Given the fundamental unit $\displaystyle U_{29} =\tfrac{5+\sqrt{29}}{2}$ and,
$$\big(U_{29}\big)^3=70+13\sqrt{29},\quad \text{thus}\;\;\color{blue}{70}^2-29\cdot\color{blue}{13}^2=-1$$
$$\big(U_{29}\big)^6=9801+1820\sqrt{29},\quad \text{thus}\;\;\color{blue}{9801}^2-29\cdot1820^2=1$$
$$2^6\left(\big(U_{29}\big)^6+\big(U_{29}\big)^{-6}\right)^2 =\color{blue}{396^4}$$
we find those integers all over Ramanujan's,
$$\frac{1}{\pi} = \frac{2 \sqrt 2}{\color{blue}{9801}} \sum_{k=0}^\infty \frac{(4k)!}{k!^4} \frac{29\cdot\color{blue}{70\cdot13}\,k+1103}{\color{blue}{(396^4)}^k}$$
The same integers appear in related pi formulas in this post.
II. A similar thing happens with the Chudnovsky formula. I knew that H. Chan explored $x^2-3dy^2 = 1$ in relation to $j(\tau)$. Given the fundamental unit for $n=3d=3\times163=489$,
$$U_n =u+v\sqrt{489} =7592629975+343350596\sqrt{489} = \big(35573\sqrt{3}+4826\sqrt{163}\big)^2$$
so $u^2-489v^2=1$. The units had a common form for $d=19,43,67,163$, for the last being,
$$U_n = \left(\tfrac{1}{18}(\color{brown}{640320}-6)\sqrt{3}+4826\sqrt{163}\right)^2$$
and some experimentation yielded,
$$3\sqrt{3}\big(U_n^{1/2}-U_n^{-1/2}\big)+6 = \color{brown}{640320}$$
$$3\sqrt{3}\big(U_n^{1/2}-U_n^{-1/2}\big)+18 = \color{brown}{2^2\cdot3^3\cdot7^2\cdot11^2}$$
$$\sqrt{163}\big(U_n^{1/2}+U_n^{-1/2}\big) = \color{brown}{2^2\cdot19\cdot127\cdot163}$$
and the Chudnovsky formula,
$$12\sum_{k=0}^\infty (-1)^k \frac{(6k)!}{k!^3(3k)!}  \frac{\color{brown}{2\cdot3^2\cdot7\cdot11\cdot19\cdot127\cdot163}\,k+13591409}{(\color{brown}{640320}^3)^{k+1/2}} = \frac{1}{\pi}$$
The same thing happens with the other $d$, for example,
$$U_{201} = \left(\tfrac{1}{18}(\color{brown}{5280}-6)\sqrt{3}+62\sqrt{67}\right)^2$$
so it is not a fluke. Plus, these $U_n$ are denominators in Ramanujan-Sato pi formulas of level 9.
Q. Anyone knows the reason why a fundamental unit $U_{n}$ would appear in a pi formula?

REPLY [6 votes]: I think is simple. All are due to algebraic units. If $z=a+b\sqrt{d}$ is a unit and $N(z)=a^2-d\cdot b^2$, then $N(z)=1$ (unit). Hence if we deal with algebraic units, then $N(z)=1=a^2-d\cdot b^2$, is a Pell equation.
See paper [1] (the knowledge of elliptic singular modulus $k_r$ requires the knowledge of algebraic units).
It have been proven that most of these formulas (Ramanujan formulas for $1/\pi$, $1/\pi^2$, etc) rise from elliptic functions (see [3],[5],[6]). For example:
Set
$$
\phi(z):={}_3F_2\left(\frac{1}{2},\frac{1}{2},\frac{1}{2};1,1,z\right)=\frac{4K^2\left(\sqrt{\frac{1}{2}\left(1-\sqrt{1-z}\right)}\right)}{\pi^2}
$$ 
Then we ask whether exist constants $a_1,b_1$ and $g$ such that
$$
\sum^{\infty}_{n=0}\frac{\left(\frac{1}{2}\right)_n^3}{(n!)^3}z^n(a_1n+b_1)=\frac{g}{\pi}\leftrightarrow b_1\phi(z)+a_1\phi'(z)=\frac{g}{\pi}.
$$
Setting $w=\sqrt{\frac{1}{2}\left(1-\sqrt{1-z}\right)}$, then $1-2w^2=\sqrt{1-z}$.
Also by direct calculation
$$
\sum^{\infty}_{n=0}\frac{\left(\frac{1}{2}\right)_n^3}{(n!)^3}4^n(w-w^2)^n(a_1n+b_1)=\frac{4K(w)\left[a_1E(w)+(b_1-a_1(1-w)-2b_1w)K(w)\right]}{\pi^2(1-2w)}
$$
Here ofcourse $K(w)$ and $E(w)$ are the complete elliptic integrals of the first and second kind. Also
$$
\alpha(r)=\frac{\pi}{4K^2(k_r)}-\sqrt{r}\left(\frac{E(k_r)}{K(k_r)}-1\right)
$$
where $k_r$ is the elliptic singular modulus i.e the solution of
$$
\frac{K(\sqrt{1-k_r^2})}{K(k_r)}=\sqrt{r}, r>0.
$$
Also
$$
\frac{dK(x)}{dx}=\frac{E(x)}{x(1-x^2)}-\frac{K(x)}{x}
$$
Hence setting $w=k_r$, $a_1=1$ and $b_1=\left(\frac{\alpha(r)}{\sqrt{r}}-k_r^2\right)\left(1-2k_r^2\right)^{-1}$ we arrive to the general formula
$$
\sum^{\infty}_{n=0}\frac{\left(\frac{1}{2}\right)_n^3}{(n!)^3}4^n(k_rk'_r)^{2n}\left(n+\frac{\alpha(r)-k_r^2\sqrt{r}}{\sqrt{r}(1-2k_r^2)}\right)=\frac{1}{\pi\sqrt{r}(1-2k_r^2)}
$$
Evaluations of $k_r$ and $\alpha(r)$ can given for positive rationals $r$. For example with $r=2$, we have $k_2=\sqrt{2}-1$, $\alpha(2)=\sqrt{2}-1$. Hence we get the next formula
$$
\sum^{\infty}_{n=0}\frac{\left(\frac{1}{2}\right)_n^3}{(n!)^3}(40\sqrt{2}-56)^{n}\left(n+\frac{2}{7}-\frac{1}{7\sqrt{2}}\right)=\frac{8+5\sqrt{2}}{14\pi}
$$
For $r$ positive integer we can find $k_r$ and $\alpha(r)$ using methods of [1],[2]. For a table of values of $k_r$ from r=1 to 100 see [4]. For evaluations of $\alpha(r)$ see[5]
[1]: Mark B. Villarino: "Ramanujan most singular modulus". arXiv:math/0308028v4 [math.HO] 2005.
[2]: D. Broadhurst. "Solutions by radicals at Singular Values $k_N$ from New Class Invariants for $N\equiv3(\textrm{mod})8$". arXiv:0807.2976 [math-ph], (2008).
[3]: N.D. Bagis, M.L. Glasser. "Ramanujan type $1/\pi$ approximation formulas". Journal of Number Theory, Elsevier., (2013)
[4]: J.M. Borwein, M.L. Glasser, R.C. McPhedran, J.G. Wan, I.J. Zucker. "Lattice Sums Then and Now". Cambridge University Press. New York, (2013). 
[5]: J.M. Borwein and P.B. Borwein. "Pi and the AGM". 1987 ed., New York: John Wiley and Sons Inc., (1987)
[6]: N.D. Bagis. "A General Method for Constructing Ramanujan-Type Formals for Powers of $1/\pi$". The Mathematica Journal. Vol. 15., (2013)
...CONTINUED
From Chan Huang's Unit Theorem we have that: If $n\equiv 2(mod4)$, then $k_n$ is unit.
From relation 
$$
\frac{(k'_n)^2}{k_n}=2g_n^{12}\textrm{, }k'_n=\sqrt{1-k_n^2}
$$
where $g_n$ is Weber's invariant:
$$
g_n=2^{-1/4}q^{-1/24}\prod^{\infty}_{m=0}(1-q^{2m+1})\textrm{, }q=e^{-\pi\sqrt{n}}.
$$ 
Now if $\delta_1,\delta_2,\ldots,\delta_{\nu(-8n)}$ are the distinct odd divisors of $-8n$ and $\delta'_l$ are the complimentary divisors of $\delta_l$, such $\delta'_l\delta_l=-8n$
$$
g_{2n}=\prod^{\nu(-8n)}_{l=1}\left(\frac{T_l+U_l\sqrt{\delta_l}}{2}\right)^{\nu(\delta_l)\nu(\delta'_l)/\nu(-8n)},
$$
where $\nu(n)$ is the number of properly primitive classes of discriminant of $n$ and $(T_l,U_l)$ is the minimal solution of the Pell equation 
$$
x^2-\delta_l y^2=4
$$
NOTES.
LEMMA 1.
If

$uv:=g_n^6$
$2U:=u^2+u^{-2}$, $2V=v^2+v^{-2}$
$W=\sqrt{U^2+V^2-1}$,
$2S=U+V+W+1$, then

$$
k_n^2=(\sqrt{S}-\sqrt{S-1})^2(\sqrt{S-U}-\sqrt{S-U-1})^2(\sqrt{S-V}-\sqrt{S-V-1})^2\times
$$
$$
\times(\sqrt{S-W}-\sqrt{S-W-1})^2.
$$
LEMMA 2.
If
$\sqrt{\alpha}:=\sqrt{ab}+\sqrt{(a+1)(b-1)}$
$\sqrt{\beta}:=\sqrt{cd}+\sqrt{(c-1)(d-1)}$, then
$$
x_1:=(\sqrt{a+1}-\sqrt{a})(\sqrt{b}-\sqrt{b-1})(\sqrt{c}-\sqrt{c-1})(\sqrt{d}-\sqrt{d-1})
$$
$$
x_2:=-(\sqrt{a+1}+\sqrt{a})(\sqrt{b}+\sqrt{b-1})(\sqrt{c}+\sqrt{c-1})(\sqrt{d}+\sqrt{d-1})
$$
are roots of 
$$
x-x^{-1}=2(\sqrt{\alpha\beta}+\sqrt{(\alpha+1)(\beta-1)})
$$