The standard reason why $e^{\pi\sqrt{N}}$ is a near integer for some $N$ is that there is some modular function $f$ with $q$-expansion $q^{-1} + O(q)$, such that substituting $\tau = \frac{1 + i\sqrt{N}}{2}$ (or perhaps $\frac{i\sqrt{N}}{2}$) and $q = e^{2 \pi i \tau}$ into the $q$-expansion of $f$ yields a rational integer. If $N$ is sufficiently large, positive powers of $q$ are very small, so the initial term $q^{-1} = e^{-\pi i (1 + i\sqrt{N})} = -e^{\pi\sqrt{N}}$ is large and very close to the rational integer. We usually see the phenomenon with $f$ as the $j$-function, but as Frictionless Jellyfish pointed out, there are other choices.

You might ask why a modular function would yield an integer when fed a quadratic imaginary input, and the answer seems to come from the theory of complex multiplication, i.e., elliptic curves whose endomorphism rings are strictly larger than the integers. I'll start by outlining the usual picture with the $j$ function, and then switch to moduli of symmetrized diagrams of curves.

**Class number one**

Given an elliptic curve $E$ over the complex numbers, we can choose a lattice $\Lambda \subset \mathbf{C}$ such that $E \cong \mathbf{C}/\Lambda$ as a complex manifold (and as an analytic group). $\Lambda$ is uniquely determined by this property up to complex rescaling, also known as homothety. The endomorphism ring of $E$ is therefore isomorphic to the endomorphism ring of $\Lambda$, which is a discrete subring of $\mathbf{C}$ and is either $\mathbf{Z}$ or the integers in a quadratic imaginary extension $K$ of $\mathbf{Q}$. In the latter case, $E$ is said to have complex multiplication (or "$E$ is a CM curve"), and $\Lambda$ is a complex multiple of a fractional ideal in $K$. Two fractional ideals in $K$ yield isomorphic curves if and only if they are related by a rescaling, i.e., by a principal ideal. This yields a bijection between isomorphism classes of elliptic curves with complex multiplication by the ring of integers in $K$, and elements of the ideal class group of $K$. These sets are finite.

For any complex elliptic curve $E$ and any ring-theoretic automorphism $\sigma$ of $\mathbf{C}$, we can define $E^\sigma$ as the curve you get by applying $\sigma$ to the coefficients of the Weierstrass equation defining $E$. Since the $j$-invariant is a rational function in the coefficients of the Weierstrass equation, $j(E^\sigma) = j(E)^\sigma$. Since $E^\sigma$ and $E$ have isomorphic endomorphism rings, the conclusion of the above paragraph implies the automorphism group of $\mathbf{C}$ acts on the set of CM curves, and their $j$-invariants, with finite orbits. In particular, $[\mathbf{Q}(j(E)):\mathbf{Q}]$ is bounded above by the class number of $K$ (and with more work, we find that we have equality). The fact that $j(E)$ is an algebraic integer can be proved in several different ways: see section II.6 of Silverman's *Advanced Topics in the Arithmetic of Elliptic Curves*. My preferred method is showing that $j$ is the solution to lots of modular equations (which yield monic polynomials).

We are left with the problem of finding CM elliptic curves whose endomorphism rings have class number one, but this is equivalent to finding lattices $\Lambda \subset \mathbf{C}$ that are the rings of integers of class number one imaginary quadratic fields. By work of Heegner, Stark, and Baker, we get the usual list: $N = 163, 67, 43, 19, \dots$, and the first few terms yield near-integers.

**Symmetrized diagrams**

For any prime $p$, there is an affine curve $Y_0(p)$ roughly parametrizing triples $(E, E', \phi)$, where $\phi: E \to E'$ is a degree $p$ isogeny of elliptic curves. Equivalently, points on this space correspond to pairs of (homothety classes of) lattices, such that one is an index $p$ sublattice of the other. I use the term "roughly" because the presence of extra automorphisms prevents the formation of a universal family over the parameter space, so the affine curve is only a coarse moduli space. The Fricke involution switches $E$ with $E'$, and sends $\phi$ to its dual isogeny. The quotient is the curve $Y_0^+(p)$, roughly parametrizing unordered pairs of elliptic curves, with dual degree $p$ isogenies between them. It is possible to consider level structures with more complicated automorphisms, but I'll stick with primes for now.

Based on the class number one discussion above, we want a function $f$ that attaches to each such unordered pair a complex number such that:

- $f$ has $q$-expansion $q^{-1} + O(q)$.
- For any ring-theoretic automorphism $\sigma$ of $\mathbf{C}$, we have the compatibility: $$f(\{E,E'\},\{\phi, \bar{\phi} \})^\sigma = f(\{E^\sigma,{E'}^\sigma \},\{\phi^\sigma, \bar{\phi}^\sigma \}).$$ This implies the value of $f$ is algebraic when $E$ and $E'$ are CM.
- $f$ should satisfy enough modular equations (or some other condition that yields integrality).

By a theorem of Cummins and Gannon, these conditions taken together imply that $f$ is a normalized Hauptmodul for a genus zero curve. In particular, $p$ must be one of the fifteen supersingular primes: 2,3,5,7,11,13,17,19,23,29,31,41,47,59,71.

Now, suppose we have a class number two field, such as $\mathbf{Q}(\sqrt{-58})$. We want a supersingular prime $p$, and an unordered pair of fractional ideals in the field, one index $p$ in the other, that is stable (up to simultaneous homothety) under the action of the automorphism group of $\mathbf{C}$ on the Weierstass coefficients of the quotient elliptic curves. Ideally, we would like there to be only one homothety class of unordered pairs, so $\operatorname{Aut} \mathbf{C}$ will automatically act trivially.

For the case at hand, the fractional ideal $(2,-i\sqrt{58})$ has index 2 in the ring of integers, its square is $(2)$, and it is the only index 2 fractional ideal. If we take $p=2$, we find that the unordered pair $\{ 1, (2,-i\sqrt{58}) \}$ represents the only homothety class of pairs of fractional ideals of index 2. This yields the result that Frictionless Jellyfish pointed out, that if $f_{2A} = q^{-1} + 4372q + 96256q^2 + \dots$ is the normalized Hauptmodul of $X_0^+(2)$, then $f_{2A}(\frac{1+i\sqrt{58}}{2}) \in \mathbf{Z}$. When $q=e^{-\pi \sqrt{58}}$, the terms with positive powers of $q$ in the expansion of $f_{2A}$ are small enough to make $q^{-1}$ close to an integer.

**Powers**

We still need to figure out why $e^{\pi\sqrt{232}}$, the square of $e^{\pi \sqrt{58}}$, is also near an integer. The easy answer is: if we square $f_{2A}$, we get $q^{-2} + 8744 + O(q)$, which is an integer when $q=e^{-\pi \sqrt{58}}$. The $O(q)$ terms here are still small enough to make $q^{-2} = e^{\pi \sqrt{232}}$ very close to an integer. You get a similar phenomenon with 88 and 148.

If you want to ask about $e^{3\pi \sqrt{58}}$, which is an integer minus $1.5 \times 10^{-4}$, a more sophisticated answer can be extracted from Alison Miller's answer to this question. The normalized Hauptmodul for $X_0^+(2)$ is a replicable function, meaning its coefficients satisfy a certain infinite collection of recurrences, introduced by Conway and Norton when studying monstrous moonshine. These recurrences are equivalent to the existence of certain modified Hecke operators $T_n$, such that $n \cdot T_nf_{2A}$ is an integer-coefficient polynomial in $f_{2A}$ with $q$-expansion $q^{-n} + O(q)$. The coefficients in the $O(q)$ part get big as $n$ increases, so powers of $e^{\pi \sqrt{58}}$ eventually drift away from integers.

(N.B.: The 2A in $f_{2A}$ refers to a conjugacy class in the monster simple group. There is a distinguished graded representation of the monster for which the trace of identity is $j-744$ and the trace of a 2A element is $f_{2A}$.)