If f (n) is an arithmetic function (i.e. a complex-valued function of the integers or natural numbers), then a convergent infinite series of the form:

or of the form:

where the ak ∈ C, is called a Ramanujan expansion[12] of f (n).
Ramanujan found expansions of some of the well-known functions of number theory. All of these results are proved in an "elementary" manner (i.e. only using formal manipulations of series and the simplest results about convergence).[13][14][15]
The expansion of the zero function depends on a result from the analytic theory of prime numbers, namely that the series

converges to 0, and the results for r(n) and r′(n) depend on theorems in an earlier paper.[16]
All the formulas in this section are from Ramanujan's 1918 paper.
Generating functions
The generating functions of the Ramanujan sums are Dirichlet series:

is a generating function for the sequence cq(1), cq(2), ... where q is kept constant, and

is a generating function for the sequence c1(n), c2(n), ... where n is kept constant.
There is also the double Dirichlet series

The polynomial with Ramanujan sum's as coefficients can be expressed with cyclotomic polynomial[17]
![{\displaystyle \sum _{n=1}^{q}c_{q}(n)x^{n-1}=(x^{q}-1){\frac {\Phi _{q}'(x)}{\Phi _{q}(x)}}=\Phi _{q}'(x)\prod _{\begin{array}{c}d\mid q\\[-4pt]d\neq q\end{array}}\Phi _{d}(x)}](//wikimedia.org/api/rest_v1/media/math/render/svg/04fbd2b895e28864e08c6f7c4f2a1eabdb66c1f1)
σk(n)
σk(n) is the divisor function (i.e. the sum of the k-th powers of the divisors of n, including 1 and n). σ0(n), the number of divisors of n, is usually written d(n) and σ1(n), the sum of the divisors of n, is usually written σ(n).
If s > 0,

Setting s = 1 gives

If the Riemann hypothesis is true, and 

d(n)
d(n) = σ0(n) is the number of divisors of n, including 1 and n itself.

where γ = 0.5772... is the Euler–Mascheroni constant.
φ(n)
Euler's totient function φ(n) is the number of positive integers less than n and coprime to n. Ramanujan defines a generalization of it, if

is the prime factorization of n, and s is a complex number, let

so that φ1(n) = φ(n) is Euler's function.[18]
He proves that

and uses this to show that

Letting s = 1,

Note that the constant is the inverse[19] of the one in the formula for σ(n).
Λ(n)
Von Mangoldt's function Λ(n) = 0 unless n = pk is a power of a prime number, in which case it is the natural logarithm log p.

Zero
For all n > 0,

This is equivalent to the prime number theorem.[20][21]
r2s(n) (sums of squares)
r2s(n) is the number of ways of representing n as the sum of 2s squares, counting different orders and signs as different (e.g., r2(13) = 8, as 13 = (±2)2 + (±3)2 = (±3)2 + (±2)2.)
Ramanujan defines a function δ2s(n) and references a paper[22] in which he proved that r2s(n) = δ2s(n) for s = 1, 2, 3, and 4. For s > 4 he shows that δ2s(n) is a good approximation to r2s(n).
s = 1 has a special formula:

In the following formulas the signs repeat with a period of 4.
![{\displaystyle {\begin{aligned}\delta _{2s}(n)&={\frac {\pi ^{s}n^{s-1}}{(s-1)!}}\left({\frac {c_{1}(n)}{1^{s}}}+{\frac {c_{4}(n)}{2^{s}}}+{\frac {c_{3}(n)}{3^{s}}}+{\frac {c_{8}(n)}{4^{s}}}+{\frac {c_{5}(n)}{5^{s}}}+{\frac {c_{12}(n)}{6^{s}}}+{\frac {c_{7}(n)}{7^{s}}}+{\frac {c_{16}(n)}{8^{s}}}+\cdots \right)&&s\equiv 0{\pmod {4}}\\[6pt]\delta _{2s}(n)&={\frac {\pi ^{s}n^{s-1}}{(s-1)!}}\left({\frac {c_{1}(n)}{1^{s}}}-{\frac {c_{4}(n)}{2^{s}}}+{\frac {c_{3}(n)}{3^{s}}}-{\frac {c_{8}(n)}{4^{s}}}+{\frac {c_{5}(n)}{5^{s}}}-{\frac {c_{12}(n)}{6^{s}}}+{\frac {c_{7}(n)}{7^{s}}}-{\frac {c_{16}(n)}{8^{s}}}+\cdots \right)&&s\equiv 2{\pmod {4}}\\[6pt]\delta _{2s}(n)&={\frac {\pi ^{s}n^{s-1}}{(s-1)!}}\left({\frac {c_{1}(n)}{1^{s}}}+{\frac {c_{4}(n)}{2^{s}}}-{\frac {c_{3}(n)}{3^{s}}}+{\frac {c_{8}(n)}{4^{s}}}+{\frac {c_{5}(n)}{5^{s}}}+{\frac {c_{12}(n)}{6^{s}}}-{\frac {c_{7}(n)}{7^{s}}}+{\frac {c_{16}(n)}{8^{s}}}+\cdots \right)&&s\equiv 1{\pmod {4}}{\text{ and }}s>1\\[6pt]\delta _{2s}(n)&={\frac {\pi ^{s}n^{s-1}}{(s-1)!}}\left({\frac {c_{1}(n)}{1^{s}}}-{\frac {c_{4}(n)}{2^{s}}}-{\frac {c_{3}(n)}{3^{s}}}-{\frac {c_{8}(n)}{4^{s}}}+{\frac {c_{5}(n)}{5^{s}}}-{\frac {c_{12}(n)}{6^{s}}}-{\frac {c_{7}(n)}{7^{s}}}-{\frac {c_{16}(n)}{8^{s}}}+\cdots \right)&&s\equiv 3{\pmod {4}}\\\end{aligned}}}](//wikimedia.org/api/rest_v1/media/math/render/svg/9c73cbb2db1296446635b8a15015e7b4b6640743)
and therefore,
![{\displaystyle {\begin{aligned}r_{2}(n)&=\pi \left({\frac {c_{1}(n)}{1}}-{\frac {c_{3}(n)}{3}}+{\frac {c_{5}(n)}{5}}-{\frac {c_{7}(n)}{7}}+{\frac {c_{11}(n)}{11}}-{\frac {c_{13}(n)}{13}}+{\frac {c_{15}(n)}{15}}-{\frac {c_{17}(n)}{17}}+\cdots \right)\\[6pt]r_{4}(n)&=\pi ^{2}n\left({\frac {c_{1}(n)}{1}}-{\frac {c_{4}(n)}{4}}+{\frac {c_{3}(n)}{9}}-{\frac {c_{8}(n)}{16}}+{\frac {c_{5}(n)}{25}}-{\frac {c_{12}(n)}{36}}+{\frac {c_{7}(n)}{49}}-{\frac {c_{16}(n)}{64}}+\cdots \right)\\[6pt]r_{6}(n)&={\frac {\pi ^{3}n^{2}}{2}}\left({\frac {c_{1}(n)}{1}}-{\frac {c_{4}(n)}{8}}-{\frac {c_{3}(n)}{27}}-{\frac {c_{8}(n)}{64}}+{\frac {c_{5}(n)}{125}}-{\frac {c_{12}(n)}{216}}-{\frac {c_{7}(n)}{343}}-{\frac {c_{16}(n)}{512}}+\cdots \right)\\[6pt]r_{8}(n)&={\frac {\pi ^{4}n^{3}}{6}}\left({\frac {c_{1}(n)}{1}}+{\frac {c_{4}(n)}{16}}+{\frac {c_{3}(n)}{81}}+{\frac {c_{8}(n)}{256}}+{\frac {c_{5}(n)}{625}}+{\frac {c_{12}(n)}{1296}}+{\frac {c_{7}(n)}{2401}}+{\frac {c_{16}(n)}{4096}}+\cdots \right)\end{aligned}}}](//wikimedia.org/api/rest_v1/media/math/render/svg/05f46864626998578e42ff8a6a09e0fc045da146)
r′2s(n) (sums of triangles)
is the number of ways n can be represented as the sum of 2s triangular numbers (i.e. the numbers 1, 3 = 1 + 2, 6 = 1 + 2 + 3, 10 = 1 + 2 + 3 + 4, 15, ...; the n-th triangular number is given by the formula nn + 1/2.)
The analysis here is similar to that for squares. Ramanujan refers to the same paper as he did for the squares, where he showed that there is a function
such that
for s = 1, 2, 3, and 4, and that for s > 4,
is a good approximation to 
Again, s = 1 requires a special formula:

If s is a multiple of 4,
![{\displaystyle {\begin{aligned}\delta '_{2s}(n)&={\frac {({\frac {\pi }{2}})^{s}}{(s-1)!}}\left(n+{\frac {s}{4}}\right)^{s-1}\left({\frac {c_{1}(n+{\frac {s}{4}})}{1^{s}}}+{\frac {c_{3}(n+{\frac {s}{4}})}{3^{s}}}+{\frac {c_{5}(n+{\frac {s}{4}})}{5^{s}}}+\cdots \right)&&s\equiv 0{\pmod {4}}\\[6pt]\delta '_{2s}(n)&={\frac {({\frac {\pi }{2}})^{s}}{(s-1)!}}\left(n+{\frac {s}{4}}\right)^{s-1}\left({\frac {c_{1}(2n+{\frac {s}{2}})}{1^{s}}}+{\frac {c_{3}(2n+{\frac {s}{2}})}{3^{s}}}+{\frac {c_{5}(2n+{\frac {s}{2}})}{5^{s}}}+\cdots \right)&&s\equiv 2{\pmod {4}}\\[6pt]\delta '_{2s}(n)&={\frac {({\frac {\pi }{2}})^{s}}{(s-1)!}}\left(n+{\frac {s}{4}}\right)^{s-1}\left({\frac {c_{1}(4n+s)}{1^{s}}}-{\frac {c_{3}(4n+s)}{3^{s}}}+{\frac {c_{5}(4n+s)}{5^{s}}}-\cdots \right)&&s\equiv 1{\pmod {2}}{\text{ and }}s>1\end{aligned}}}](//wikimedia.org/api/rest_v1/media/math/render/svg/f201a53880e4c929d3fa3af66848dbdf3bb0d420)
Therefore,
![{\displaystyle {\begin{aligned}r'_{2}(n)&={\frac {\pi }{4}}\left({\frac {c_{1}(4n+1)}{1}}-{\frac {c_{3}(4n+1)}{3}}+{\frac {c_{5}(4n+1)}{5}}-{\frac {c_{7}(4n+1)}{7}}+\cdots \right)\\[6pt]r'_{4}(n)&=\left({\frac {\pi }{2}}\right)^{2}\left(n+{\frac {1}{2}}\right)\left({\frac {c_{1}(2n+1)}{1}}+{\frac {c_{3}(2n+1)}{9}}+{\frac {c_{5}(2n+1)}{25}}+\cdots \right)\\[6pt]r'_{6}(n)&={\frac {({\frac {\pi }{2}})^{3}}{2}}\left(n+{\frac {3}{4}}\right)^{2}\left({\frac {c_{1}(4n+3)}{1}}-{\frac {c_{3}(4n+3)}{27}}+{\frac {c_{5}(4n+3)}{125}}-\cdots \right)\\[6pt]r'_{8}(n)&={\frac {({\frac {\pi }{2}})^{4}}{6}}(n+1)^{3}\left({\frac {c_{1}(n+1)}{1}}+{\frac {c_{3}(n+1)}{81}}+{\frac {c_{5}(n+1)}{625}}+\cdots \right)\end{aligned}}}](//wikimedia.org/api/rest_v1/media/math/render/svg/a99651ffc1270d3aa90ff15797090801ad2a5129)
Sums
Let

Then for s > 1,
