1 Introduction

1.1 Euclidean algorithm (classical version)

The Euclidean algorithm—referred to as ‘\( \mathrm {EA}^{(\mathrm {sub})} \)’ in the sequel—for the computation of the greatest common divisor (gcd) of two positive integers a and b, has been described as ‘the oldest non-trivial algorithm that has survived to the present day’ by Knuth [16, p. 318]. In its most basic form the algorithm proceeds by replacing the input tuple (ab) by \((a-b,b)\) if \(a<b\) (‘Case A’) and \((a,b-a)\) if \(a\ge b\) (‘Case B’) until one of the arguments becomes zero (‘Case C’), in which case the gcd of the original input is given by the other argument. (There is some leeway in describing the algorithm and we shall choose what is convenient for our exposition rather than what is historically most accurate; the reader is referred to loc. cit. for a more detailed discussion of that matter.) For instance, on the input (11, 3), the algorithm takes the following six steps:

$$\begin{aligned} \begin{aligned} (11,3)&\mapsto (8,3) \mapsto (5,3) {\mathop {\mapsto }\limits ^{*}} (2,3) {\mathop {\mapsto }\limits ^{*}} (2,1) \\&\mapsto (1,1) {\mathop {\mapsto }\limits ^{*}} ({\underline{1}},0) \quad (\text {hence, }\gcd (11,3) = {\underline{1}}), \end{aligned} \end{aligned}$$
(1.1)

where the asterisks (\(*\)) mark the positions where the algorithm switches between cases. Observe that the number 11/3 has the continued fraction expansion

$$\begin{aligned} \frac{11}{3} = 3 + \frac{1}{ 1 + \frac{1}{ 2 } } \, . \end{aligned}$$
(1.2)

and \(6 = 3 + 1 + 2\) is the sum of the partial quotients herein.

If one modifies Case A of \( \mathrm {EA}^{(\mathrm {sub})} \) as to replace (ab) by \((a-B,b)\), where B is the largest multiple of b not exceeding a, and modifies Case B similarly, then the modified algorithm skips all steps (\(\mapsto \)) not marked with an asterisk in the above example; this amounts to precisely 3 steps which is also the number of partial quotients in the continued fraction expansion (1.2); we shall refer to this version of \( \mathrm {EA}^{(\mathrm {sub})} \) by \( \mathrm {EA}^{(\mathrm {div})} \).

It is easy to see that the correspondence of number of steps on the input (ab) and properties of the continued fraction expansion

$$\begin{aligned} \frac{a}{b} = [0; a_1, \ldots , a_n] :=0 + \frac{1}{ a_1 + \frac{1}{ a_2 + \cdots \frac{}{ \cdots + \frac{1}{ a_n } } } } \end{aligned}$$
(1.3)

of \(a/b\in [0,1)\) (where \(n\in \mathbb {N}_0\) and the so-called partial quotients \(a_1,a_2,\ldots ,a_n\) are positive integers and \(a_n\ge 2\)) holds in general, i.e.,

  • the number of steps taken by \( \mathrm {EA}^{(\mathrm {sub})} \) when applied to (ab) (or any tuple (kakb) with some positive integer k) is \(a_1 + a_2 + \cdots + a_n\) (see Fig. 1a for a plot of its behavior), and

  • the number of steps taken by \( \mathrm {EA}^{(\mathrm {div})} \) is n. We denote this number by s(a/b). (See Fig. 1b for a plot of its behavior.)

Fig. 1
figure 1

The number of steps of \( \mathrm {EA}^{(\mathrm {sub})} \), \( \mathrm {EA}^{(\mathrm {div})} \) & \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \) when applied to all reduced \(a/b\in [0,1)\cap \mathbb {Q}\) with \(1\le b\le 100\)

1.2 Variants of the Euclidean algorithm

Several other variants of the Euclidean algorithm have been considered in the literature (see, e.g., [27, 28] for a selection). For the most part, they arise (ignoring some technicalities) from modifying the distinguishing conditions of the cases A and B as introduced in Sect. 1.1. Here we discuss only one such variant. In fact, for convenience, we restrict our discussion to only stating a variant that is more similar in spirit to \( \mathrm {EA}^{(\mathrm {div})} \) rather than \( \mathrm {EA}^{(\mathrm {sub})} \). To obtain this variant—referred to as \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \) in the sequel—modify Case A of \( \mathrm {EA}^{(\mathrm {div})} \) to replace the input (ab) by \((B-a,b)\), where B is the smallest multiple of b not smaller than a and make a similar modification to Case B. Given this modification, our example (1.1) takes the shape \( (11,3) {\mathop {\mapsto }\limits ^{*}} (1,3) {\mathop {\mapsto }\limits ^{*}} ({\underline{1}},0) \).

Once more, one can associate a certain continued fraction expansion of a number \(a/b\in [0,1)\) to the behaviour of the algorithm on the input (ab). The particular continued fraction expansion relevant in this case is often called minus continued fraction expansionFootnote 1 and takes the shape

$$\begin{aligned} \frac{a}{b} = \llbracket 1; b_1, \ldots , b_m \rrbracket :=1 - \frac{1}{ b_1 - \frac{1}{ b_2 - \cdots \frac{}{ \cdots - \frac{1}{ b_m } } } } \, , \end{aligned}$$
(1.4)

where \(m\in \mathbb {N}\) and \(b_1,b_2,\ldots ,b_m\ge 2\) are integers. When expanding a/b as in (1.4), then \(m+1\) can be seen to be the number of steps taken by \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \) on the input (ab). We shall write \(\ell (a/b)\) for the number m from (1.4) in the sequel. (See Fig. 1c for a plot of \(\ell (a/b)\).) For further background on continued fractions we refer to [20].

1.3 Asymptotics for the number of steps of Euclidean algorithms

It is an interesting question to study statistical properties of the number of steps of the Euclidean algorithm (and its variants), or—equivalently—distribution properties of continued fractions. It was Heilbronn [12] who first identified the principal term of the asymptotics for the average number of steps in the case of the classical Euclidean algorithm, the average being taken over numerators:

$$\begin{aligned} {\frac{1}{\varphi (b)} \sum _{\begin{array}{c} a\le b \\ \gcd (a,b)=1 \end{array}} s\biggl ( \frac{a}{b} \biggr ) = A_1 \log b + O( (\log \log b)^4 )\quad (\text {as~} b\rightarrow \infty );} \end{aligned}$$

here (\(n\in \mathbb {N}\)) is Euler’s totient function and \(A_1\) is an explicitly given non-zero constant.Footnote 2 For the same average, an asymptotic formula with two significant terms was obtained later by Porter [21]:

$$\begin{aligned} {\frac{1}{\varphi (b)} \sum _{\begin{array}{c} a\le b \\ \gcd (a,b)=1 \end{array}} s\biggl ( \frac{a}{b} \biggr ) = A_1 \log b + A_2 + O_\epsilon ( b^{-1/6+\epsilon } );} \end{aligned}$$

here \(A_1\) is as before and \(A_2\) is also an explicitly given non-zero constant. Bykovskiĭ and Frolenkov [6] have recently obtained a generalisation of this and obtained an improved error term.

Considering averages over both numerators and denominators, an asymptotic formula with power-law fall-off in the error term was obtained by Vallée [27] through the use of probability theory and ergodic-theoretic methods. This was improved by Ustinov [24], who obtained an asymptotic formula with better fall-off in the error term than the one that can be derived from Porter’s result:

$$\begin{aligned} {\frac{1}{\#{\mathscr {F}}(Q)} \mathop { \sum _{b\le Q} \sum _{a\le b} }_{ \gcd (a,b)=1 } s\biggl ( \frac{a}{b} \biggr ) = B_1 \log Q + B_2 + O( (\log Q)^5 / Q ),} \end{aligned}$$
(1.5)

where

$$\begin{aligned} B_1 = \frac{\log 2}{2\zeta (2)}, \quad B_2 = \frac{\log 2}{4\zeta (2)} \biggl ( 3 \log 2 + 4 \gamma - 2 \frac{\zeta '(2)}{\zeta (2)} - 3 \biggr ) - \frac{1}{4}, \end{aligned}$$

\(\gamma \) denotes the Euler–Mascheroni constant, \(\zeta \) is the Riemann zeta function, and

denotes the set of Farey fractions of order Q. In this regard it is worth noting that another natural way of averaging is over all pairs (ab) with \(1\le a\le b \le Q\) without assuming coprimality of a and b. However, this situation is easily covered using (1.5) and Möbius inversion.

While examining the statistical properties of different variations of the Euclidean algorithm, Vallée [28] obtained also the leading term of the asymptotic formula for the expectation of the number of steps of the by-excess Euclidean algorithm (and hence for the average length of minus continued fractions). This was improved by Zhabitskaya [30] (following the approach of Ustinov [24]), a few years later, who showed that

$$\begin{aligned} {\frac{1}{\#{\mathscr {F}}(Q)} \mathop { \sum _{b\le Q} \sum _{a\le b} }_{ \gcd (a,b)=1 } \ell \biggl ( \frac{a}{b} \biggr ) {=} C_1 (\log Q)^2 {+} C_2 \log Q {+} C_3 {+} O( (\log Q)^6 / Q ),} \end{aligned}$$
(1.6)

where \(C_1, C_2, C_3\) are explicitly given non-zero constants, the first two being given by

$$\begin{aligned} C_1 = \frac{1}{2\zeta (2)}, \quad C_2 = \frac{1}{\zeta (2)} \biggl ( 2\gamma - \frac{3}{2} - 2 \frac{\zeta '(2)}{\zeta (2)} \biggr ), \end{aligned}$$
(1.7)

and the value of \(C_3\) being given by a somewhat longer, yet similar expression which we omit here. Both error terms in (1.5) and (1.6) have been improved to \({O( (\log Q)^3 / Q )}\) by Frolenkov [10] who incorporated ideas of Selberg from the elementary proof of the prime number theorem.

For more results regarding the expectation and the variance of the number of steps of the classical and by-excess Euclidean algorithm, we also refer to the work of Baladi and Vallée [1], Bykovskiĭ [5], Dixon [8, 9], Hensley [13] and Ustinov [25, 26].

1.4 Dedekind sums

Let denote the integer part of \(\eta \in \mathbb {R}\). Then the saw-tooth function is defined as

$$\begin{aligned} ((\eta )) = {\left\{ \begin{array}{ll} \eta - \lfloor \eta \rfloor - 1/2 &{} \text {if } \eta \in \mathbb {R}\setminus \mathbb {Z}, \\ 0 &{} \text {if } \eta \in \mathbb {Z}. \\ \end{array}\right. } \end{aligned}$$

For any pair \(a,b\in \mathbb {Z}\), \(b\ne 0\), the Dedekind sumFootnote 3D(ab) is defined as

$$\begin{aligned} D(a,b) = \sum _{n\le b} \biggl ( \!\!\biggl ( \frac{n}{b} \biggr )\!\! \biggr ) \biggl ( \!\!\biggl ( \frac{na}{b} \biggr )\!\! \biggr ). \end{aligned}$$

It can be verified that \(D(a,b) = D(ka,kb)\) for any non-zero integer k. Hence, \(D(a/b) :=D(a,b)\) is well defined. Moreover, the function \(D:\mathbb {Q}\rightarrow \mathbb {Q}\) just defined is periodic with period one.

Dedekind sums originally arose in connection with the multiplier system for Dedekind’s eta function over the modular group of two by two integer matrices of determinant one [7] and also satisfy a curious reciprocity law. By means of the latter Barkan [2] and (independently) Hickerson [14] have obtained the following identity which connects Dedekind sums with continued fraction expansions:

$$\begin{aligned} D(a/b) = \frac{(-1)^n - 1}{8} + \frac{ a/b - (-1)^n [0;a_n,\ldots ,a_2,a_1] + \varSigma _\pm (a/b) }{12}; \end{aligned}$$
(1.8)

here \(a/b = [0; a_1,a_2,\ldots ,a_n]\) is as in (1.3) and

$$\begin{aligned} \varSigma _\pm (a/b) :=\sum _{j\le n} (-1)^{j-1} a_j. \end{aligned}$$
(1.9)

(See Fig. 2 for a plot of \(\varSigma _\pm \).) In particular, Hickerson employed (1.8) to prove that the set is dense in \(\mathbb {R}^2\).

Fig. 2
figure 2

Plot of \(\varSigma _\pm (a/b)\) when applied to all Farey fractions \(a/b\in [0,1]\cap \mathbb {Q}\) with \(1\le b\le 100\). Note that the average of the plotted values over the interval [0, 1/2) is clearly positive, whereas the average of the plotted values over the interval [1/2, 1) is negative

Concerning distribution properties of Dedekind sums observe that via the symmetry property \(D(x) = - D(1-x)\) it is easy to see that

$$\begin{aligned} \sum _{x\in {\mathscr {F}}(Q)} D(x) = 0. \end{aligned}$$

On the other hand, let \({\mathscr {F}}_0(Q) = {\mathscr {F}}(Q) \cap [0,1/2) \) denote ‘half’ of all Farey fractions with denominators bounded by Q. Then, on the basis of numerical evidence, it has been conjectured by Ito [15] that

$$\begin{aligned} \lim \limits _{Q\rightarrow \infty } \varSigma (Q) = +\infty , \quad \text {where}\quad \varSigma (Q) :=\frac{1}{\#{\mathscr {F}}(Q)} \sum _{x\in {\mathscr {F}}_0(Q)} D(x). \end{aligned}$$
(1.10)

For an exposition of results on Dedekind sums we refer to the classical work of Rademacher and Grosswald [22], as well as a more up-to-date survey of Girstmair [11] with a focus on distribution properties.

2 Main results

2.1 Results

One of the main results of the present work is a proof of Ito’s conjecture:

Theorem 2.1

(Ito’s conjecture is true) The statement in (1.10) holds. In fact, one even has the following stronger quantitative version:

$$\begin{aligned} \frac{1}{\#{\mathscr {F}}(Q)} \sum _{x\in {\mathscr {F}}_0(Q)} D(x) = \frac{1}{16} \log Q + O(1). \end{aligned}$$
(2.1)

The proof of Theorem 2.1 rests crucially on the following variant of (1.6) which we believe to be of independent interest:

Theorem 2.2

(Bias in \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \)) We have

$$\begin{aligned} \frac{1}{\#{\mathscr {F}}(Q)}\sum _{x\in {\mathscr {F}}_0(Q)} \ell (x) = c_1 (\log Q)^2 + c_2 \log Q + O(1), \end{aligned}$$

where \(c_1,c_2\) are non-zero constants satisfying \(2c_1 = C_1\) and \(2c_2 > C_2\) with the constants \(C_1\) and \(C_2\) given in (1.7). More precisely,

$$\begin{aligned} c_1 = \frac{1}{4\zeta (2)}, \quad c_2 = \frac{1}{2\zeta (2)} \biggl ( 2\gamma - \frac{3}{2} - 2\frac{\zeta '(2)}{\zeta (2)} + \frac{3\zeta (2)}{4} \biggr ) = \frac{C_2}{2} + \frac{3}{8}. \end{aligned}$$

The above theorem may be interpreted as a quantitative version of the statement that the length \(\ell (a/b)\) of the minus continued fraction expansion (1.4) tends to be larger on average on \({\mathscr {F}}_0(Q)\) than on \({\mathscr {F}}(Q) \setminus {\mathscr {F}}_0(Q)\) (due to \(2c_2 > C_2\); see (1.6)). This may be phrased equivalently as saying that \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \) takes longer on average for fractions in \([0,1/2)\) than it does for fractions in \([1/2,1)\).

In view of the above it seems natural to ask if similar results can be obtained for the other algorithms \( \mathrm {EA}^{(\mathrm {sub})} \) and \( \mathrm {EA}^{(\mathrm {div})} \) discussed in Sect. 1.1. This turns out to be a rather easier question. For \( \mathrm {EA}^{(\mathrm {sub})} \) one sees no difference in behaviour on \({\mathscr {F}}_0(Q)\) versus on \({\mathscr {F}}(Q) \setminus {\mathscr {F}}_0(Q)\), as should be evident from the symmetry in Fig. 1a about the vertical line through 1/2. The latter symmetry may be verified easily by noting that \(x = [0; a_1, a_2,\ldots ,a_n]\) (with \(a_1\ge 2\) so that \(x \le 1/2\)) and \(1-x = [0; 1, a_1-1, a_2,\ldots ,a_n]\) have the same sum of partial quotients, viz. identical running time when fed into \( \mathrm {EA}^{(\mathrm {sub})} \). On the other hand, an analogue of Theorem 2.2 may be obtained for \( \mathrm {EA}^{(\mathrm {div})} \):

Proposition 2.3

(Bias in \( \mathrm {EA}^{(\mathrm {div})} \)) We have

$$\begin{aligned} \frac{1}{\#{\mathscr {F}}(Q)}\sum _{x\in {\mathscr {F}}_0(Q)} s(x) = b_1 \log Q + b_2 + O((\log Q)^5 / Q), \end{aligned}$$

where \(2 b_1 = B_1\) and \(2b_2 < B_2\) with the constants \(B_1\) and \(B_2\) given from (1.5). More precisely, \(2 b_2 = B_2 - 1/2\).

Proof

This follows immediately from (1.5) and the fact that \(s(x) = s(1-x) - 1\) for \(x\in (0,1/2)\). \(\square \)

We should like to mention that Bykovskiĭ [5] has obtained an asymptotic formula for averaging s(a/q) over all a in some arbitary interval of length at most q. However, the error term in his result does not permit one to deduce Proposition 2.3.

Generalising Theorem 2.2 and Proposition 2.3 to averages over \({\mathscr {F}}\cap [0,\alpha )\) seems to be an interesting problem. However, this requires a more careful analysis and a sufficiently flexible generalisation of Lemma 4.2 below. As this seemed dispensable for our primary intent of proving Theorem 2.1, we shall address this elsewhere in forthcoming work (see also the first author’s doctoral dissertation [18]).

2.2 Plan of the paper

In the next section we show how Theorem 2.1 can be deduced from Theorem 2.2. The proof of Theorem 2.2 is rather more involved. In Sect. 4 we sketch the overall argument and show how Theorem 2.2 can be deduced from a technical proposition (Proposition 4.5). The proof of the latter is carried out in Sect. 5.

2.3 Notation

We use the Landau notation \(f(x) = O(g(x))\) and the Vinogradov notation \(f(x) \ll g(x)\) to mean that there exists some constant \(C>0\) such that \(\left| f(x)\right| \le C g(x)\) holds for all admissible values of x (where the meaning of ‘admissible’ will be clear from the context). Unless otherwise indicated, any dependence of C on other parameters is specified using subscripts. Similarly, we write ‘\(f(x) = o(g(x))\) as \(x\rightarrow \infty \)’ if g(x) is positive for all sufficiently large values of x and f(x)/g(x) tends to zero as \(x\rightarrow \infty \).

Given two coprime integers a and \(q\ne 0\) we write \({{\,\mathrm{inv}\,}}_q(a)\) for the smallest positive integer in the residue class \((a\bmod q)^{-1}\).

3 Deducing Theorem 2.1 from Theorem 2.2

Throughout this section we shall assume that Theorem 2.2 has already been proved. The main tool for deducing Theorem 2.1 from Theorem 2.2 is the formula (1.8) of Barkan and Hickerson. In this vein, recall also the definition of \(\varSigma _\pm (x)\) given in (1.9). For a number \(x\in [0,1)\) as in (1.3) let

$$\begin{aligned} \varSigma _{\text {odd} }(x) = \sum _{\begin{array}{c} i=1 \\ i\text { odd} \end{array}}^n a_i, \quad \varSigma _{\text {even}}(x) = \sum _{\begin{array}{c} i=2 \\ i\text { even} \end{array}}^n a_i. \end{aligned}$$

Then, clearly,

$$\begin{aligned} \varSigma _\pm (x) = \varSigma _{\text {odd}}(x) - \varSigma _{\text {even}}(x). \end{aligned}$$
(3.1)

The connection with minus continued fraction expansions and, thus, Theorem 2.2 arises as follows: in [29] Zhabitskaya notesFootnote 4 that it is implicit in an article of Myerson [19] that

$$\begin{aligned} \ell ( x ) = \varSigma _{\text {odd} }(x) - \epsilon (x), \end{aligned}$$
(3.2)
$$\begin{aligned} \ell (1-x) = \varSigma _{\text {even}}(x) + \epsilon (x). \end{aligned}$$
(3.3)

Here is some correction term which is related to our way of forcing uniqueness in the continued fraction expansion (1.3) by means of requiring the last partial quotient \(a_n\) to exceed 1. In fact, one can describe the value of \(\epsilon (x)\) quite precisely (see [29]), but this is not necessary for our particular application.

Corollary 3.1

We have

$$\begin{aligned} \frac{1}{\#{\mathscr {F}}(Q)}\sum _{x\in {\mathscr {F}}_0(Q)} \varSigma _\pm (x) = \frac{3}{4} \log Q + O(1). \end{aligned}$$

Proof

From (3.2) and Theorem 2.2 we deduce that

$$\begin{aligned} \frac{1}{\#{\mathscr {F}}(Q)}\sum _{x\in {\mathscr {F}}_0(Q)} \varSigma _{\text {odd}}(x) = c_1 (\log Q)^2 + c_2 \log Q + O(1). \end{aligned}$$

Moreover, by (3.3),

$$\begin{aligned} \sum _{x\in {\mathscr {F}}_0(Q)} \varSigma _{\text {even}}(x) = \sum _{x\in {\mathscr {F}}_0(Q)} \ell (1-x) + O(Q^2) = \sum _{x\in {\mathscr {F}}(Q)\setminus {\mathscr {F}}_0(Q)} \ell (x) + O(Q^2). \end{aligned}$$

On the other hand, (1.6) and Theorem 2.2 show that, after dividing by \(\#{\mathscr {F}}(Q)\), the right hand side in the above is

$$\begin{aligned} (C_1-c_1) (\log Q)^2 + (C_2-c_2) \log Q + O(1). \end{aligned}$$

In view of (3.1), the result follows from the previous considerations. \(\square \)

Proof of Theorem 2.1

Clearly it suffices to prove (2.1). To this end, observe that, by (1.8), we have \(D(x) = \varSigma _\pm (x) / 12 + O(1)\). Now (2.1) follows immediately from this and Corollary 3.1. \(\square \)

4 Proof of Theorem 2.2

Before stating the key lemmas needed for the proof of Theorem 2.2, we give a short informal sketch of the overall argument. In Sect. 4.2 we state the three key lemmas we require. The proof of Theorem 2.2 is given in Sect. 4.3.

4.1 Sketch of the proof

In proving Theorem 2.2, we adapt the approach of Zhabitskaya [30]. The idea, which goes back to Lochs [17] and Heilbronn [12], is to transfer the problem of computing the (restricted) average of the lengths of (minus) continued fractions into a problem of counting lattice points inside certain regions. By virtue of Lemmas 4.3 and 4.4 (below), the proof of Theorem 2.2 boils down to evaluating asymptotically the number of integer solutions of the system

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} \gcd (p,q) = 1, &{} p,q\ge 1, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2,\\ 2 \le n q + kp \le Q, &{} 1\le k<n. \end{array} \right. \end{aligned}$$

This amounts to counting the lattice points inside some region subject to some coprimality condition and the additional restriction \({{\,\mathrm{inv}\,}}_p(q)\le q/2\). The latter restriction is not present in [30] and complicates the overall analysis. Following [30], we split the problem of counting the solutions to the above system into five sub-cases. For every case we have to count lattice points with certain properties inside regions (see Sect. 4.3 for the details). This counting problem is solved in Proposition 4.5 and it should be apparent from the proof of Proposition 4.5 that the reason for the bias (\(2c_2 > C_2\)) in Theorem 2.2 is found within two of the considered cases. More specifically, for one of these cases, the number of lattice points to be counted is given, up to some error term, by

$$\begin{aligned} \sum _{q<Q^{1/4}} \frac{1}{q} \sum _{\begin{array}{c} q/2<b\le q\\ \gcd (b,q)=1 \end{array}}\frac{1}{q}\log \frac{Q^{1/2}}{q^2} = \sum _{q<Q^{1/4}} \frac{1}{q^2}\log \frac{Q^{1/2}}{q^2} \delta ^{+}(q), \end{aligned}$$

where \(\delta ^{+}\) is the function appearing in Lemma 4.2. The same procedure carried out for fractions greater than 1/2 leads to the same expression with \(\delta ^{+}\) being replaced by \(\delta ^{-}\). As Lemma 4.2 shows, the functions \(\delta ^{+}\) and \(\delta ^{-}\) agree everywhere except at 1 and 2; this is the reason for \(2c_2 > C_2\).

4.2 Four lemmas

Each of the following lemmas plays a crucial rôle in the proof of Theorem 2.2. In fact, in spite of its simplicity, Lemma 4.1 turns out to be particularly useful in establishing Proposition 4.5: it permits a simple, yet important modification of the considered systems, allowing us to evaluate \(R_3(U)\) and \(R_5(U)\) (to be defined below) with the required precision (see Sect. 5 for details). The relevance of Lemma 4.2 as the source of bias was already explained in Sect. 4.1. Lemmas 4.3 and 4.4 are adapted from [30, Lemma 2 in § 2.3] and allow us to translate our problem into the enumeration of the solutions of a system of inequalities (see (4.2)).

Lemma 4.1

(Inversion trick) Let \(p,q\ge 2\) be two coprime integers. Then

$$\begin{aligned} {{\,\mathrm{inv}\,}}_{p}(q)\le \frac{p}{2} \quad \text {if and only if}\quad {{\,\mathrm{inv}\,}}_{q}(p) > \frac{q}{2}. \end{aligned}$$

Proof

By coprimality, there are integers a and b such that \(aq + bp = 1\), where \(a = {{\,\mathrm{inv}\,}}_p(q) + tp\) and \(b = {{\,\mathrm{inv}\,}}_q(p) + sq\) for some integers s and t. Hence

$$\begin{aligned} {{\,\mathrm{inv}\,}}_p(q)q + {{\,\mathrm{inv}\,}}_q(p)q - qp \equiv 1 \bmod pq. \end{aligned}$$

On the other hand, the left hand side of the above is contained in the interval \((-pq,pq)\). Hence, we conclude

$$\begin{aligned} {{\,\mathrm{inv}\,}}_p(q)q + {{\,\mathrm{inv}\,}}_q(p)p = 1 + pq, \end{aligned}$$

from which the lemma follows. \(\square \)

Lemma 4.2

Let \(\varphi \) be Euler’s totient function and define for every positive integer q the counting functions

$$\begin{aligned} \delta ^-(q) = \sum _{\begin{array}{c} b \le q/2 \\ \gcd (b,q)=1 \end{array}} 1 \quad \text {and}\quad \delta ^+(q) = \sum _{\begin{array}{c} q/2 < b \le q \\ \gcd (b,q)=1 \end{array}} 1. \end{aligned}$$

Then the following assertions hold:

  1. 1.

    \(\delta ^+(1) = \delta ^-(2) = 1\);

  2. 2.

    \(\delta ^+(2) = \delta ^-(1) = 0\);

  3. 3.

    \(\delta ^+(q) = \delta ^-(q) = \varphi (q) / 2\) for \(q \ge 3\).

Proof

The assertions for \(q\le 2\) are trivial to check. For \(q\ge 3\) note that the sets

are disjoint and in bijection by means of the map \(b\mapsto q-b\). As the union of both sets contains exactly \(\varphi (q)\) elements, we are done. \(\square \)

Lemma 4.3

The sum \(N_0(Q)\) of the lengths of the minus continued fraction expansions of the numbers a/q with \(1\le a < q/2\), \(q\le Q\) is

$$\begin{aligned} N_0(Q) = T_0(Q) + O(Q^2), \end{aligned}$$

where \(T_0(Q)\) denotes the number of solutions \((a_1,q_1,a_2,q_2,m,n,a,b)\in \mathbb {N}^8\) to the following system of equalities and inequalities:

$$\begin{aligned} \left\{ \begin{array}{@{}lll@{}} a_1q_2 - a_2 q_1 = 1, &{} 1\le a_1 \le q_1, &{} 1 \le a_2 \le q_2/2, \\ n a_2 - m a_1 = a, &{} n q_2 - m q_1 = b, &{} 1 \le a< b \le Q, \\ 1 \le m< n, &{} 1\le q_1 < q_2. \end{array} \right. \end{aligned}$$
(4.1)

Proof

The claim follows mutatis mutandis from [30, pp. 1185–1186]. \(\square \)

Next, discarding an acceptable number of solutions in the process, we reduce the system (4.1) to a system with four variables.

Lemma 4.4

Let R(Q) denote the number of solutions \((p, q, n, m) \in \mathbb {N}^4\) of the system

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} \gcd (p,q) = 1, &{} p,q \ge 1, \\ {{\,\mathrm{inv}\,}}_p(q) \le p/2, \\ 2 \le n q + kp \le Q, &{} 1 \le k < n. \end{array} \right. \end{aligned}$$
(4.2)

Then, the number \(N_0(Q)\) defined as in Lemma 4.3 satisfies

$$\begin{aligned} N_0(Q) = R(Q) + O(Q^2). \end{aligned}$$

Proof

By virtue of Lemma 4.3, we only need to show that \(R(Q) = T_0(Q) + O(Q^2)\). It is convenient to exclude the solutions with \(q_1=1\) from the discussion. We claim that their number is \(O(Q^2)\) and, thus, negligible. To this end, consider first all the solutions of the system (4.1) with \(q_1=1\). The conditions in system (4.1) force that \(a_1=a_2=q_1=1\) and \(q_2=2\), reducing the system to

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} n-m=a, &{} 2n-m=b, \\ 1\le a<b\le Q, &{} 1\le m<n, \end{array} \right. \end{aligned}$$

for which one easily sees that its number of solutions is \(\ll Q^2\).

For the remainder of the proof we shall assume that \(q_1\ge 2\). We claim that this assumption also implies that \(a_1\le q_1/2\). Indeed, suppose to the contrary that there was some solution to (4.1) with \(q_1\ge 2\) and \(a_1>q_1/2\). We then deduce that

$$\begin{aligned} 2 = 2(a_1q_2-a_2q_1) \ge (q_1+1)q_2-2a_2q_1 \ge (q_1+1)q_2-q_2q_1 = q_2 > q_1, \end{aligned}$$

in contradiction with \(q_1 \ge 2\).

Upon reducing the equation \(a_1q_2-a_2q_1=1\) modulo \(q_1\), we obtain \(a_1={{\,\mathrm{inv}\,}}_{q_1}(q_2)+tq_1\) for some integer t. As \(a_1\) is positive and \(q_1<q_2\), it follows that t must vanish. Hence, \(a_1={{\,\mathrm{inv}\,}}_{q_1}(q_2)\). Consequently, \({{\,\mathrm{inv}\,}}_{q_1}(q_2)\le q_1/2\). Now consider the system

$$\begin{aligned} \left\{ \begin{array}{@{}lll@{}} \gcd (q_1,q_2) = 1, &{} 1 \le q_1<q_2, &{} {{\,\mathrm{inv}\,}}_{q_1}(q_2)\le q_1/2, \\ 2\le n q_2 - m q_1 \le Q, &{} 1 \le m < n. \end{array} \right. \end{aligned}$$
(4.3)

We now contend that the map \(\Psi \) sending solutions \(\varvec{u} = (a_1,q_1,a_2,q_2,m,n,a,b)\) of (4.1) with \(q_1\ge 2\) to solutions \(\varvec{v} = (q_1, q_2, m, n)\) of (4.3) (by means of dropping the entries \(a_1\), \(a_2\), a, and b) is a bijection. Indeed, above we have just seen that this map is well defined. To see that it is injective, suppose that \(\varvec{v}\) arises from some solution \(\varvec{u}\) of (4.1). As we have seen, \(a_1 = {{\,\mathrm{inv}\,}}_{q_1}(q_2)\) is already determined by \(\varvec{v}\). But then, by \(a_1q_2-a_2q_1 = 1\), also \(a_2\) is determined by \(\varvec{v}\). Similarly, (4.1) then yields that also a and b are determined by \(\varvec{v}\), showing that \(\Psi \) is injective.

To show that \(\Psi \) is also surjective, we start out with some solution \(\varvec{v} = (q_1, q_2, m, n)\) of (4.3) and need to exhibit some preimage of \(\varvec{v}\) under \(\Psi \). As \(q_1\) and \(q_2\) are coprime, there exist integers \(a_1\) and \(a_2\) such that \(a_1q_2-a_2q_1 = 1\). Moreover, by replacing \((a_1,a_2)\) by \((a_1+tq_1,a_2+tq_2)\) with an appropriate integer t, we may assume that \(0\le a_1<q_1\). Furthermore, define \(a = n a_2 - m a_1\) and \(b = n q_2 - m q_1\). We now show that the octuple \(\varvec{u} = (a_1,q_1,a_2,q_2,m,n,a,b)\) is the desired preimage \(\varvec{v}\) under \(\Psi \). We have shown above that \(a_1 = {{\,\mathrm{inv}\,}}_{q_1}(q_2)\). Similarly, by reducing \(a_1q_2-a_2q_1 = 1\) modulo \(q_2\), we find that \(a_2 = t_2 q_2 - {{\,\mathrm{inv}\,}}_{q_2}(q_1)\) for some integer \(t_2\). We claim that \(t_2 = 1\). To see this, first observe that

$$\begin{aligned} a_1q_2-(q_2 - {{\,\mathrm{inv}\,}}_{q_2}(q_1))q_1 \equiv a_1q_2-a_2q_1 = 1 \mod q_1q_2. \end{aligned}$$
(4.4)

From (4.3) we see that \(a_1 = {{\,\mathrm{inv}\,}}_{q_1}(q_2) \le q_1/2\) and Lemma 4.1 shows that \({{\,\mathrm{inv}\,}}_{q_2}(q_1) > q_2/2\). Therefore,

$$\begin{aligned} a_1q_2-(q_2 - {{\,\mathrm{inv}\,}}_{q_2}(q_1))q_1 \left\{ \begin{array}{@{}l@{}} > q_1q_2/2-(q_2 - q_2/2)q_1 = 0, \\ < q_1q_2. \end{array} \right. \end{aligned}$$
(4.5)

Upon combining (4.4) and (4.5) we infer that the left hand side of (4.5) is equal to one and this shows that \(a_2 = q_2 - {{\,\mathrm{inv}\,}}_{q_2}(q_1)\), as claimed. In particular, we have \(a_2 < q_2/2\). Moreover (4.3) shows that \(b \le Q\). It remains to show that \(a < b\). We have

$$\begin{aligned} q_1 a = q_1 ( n a_2 - m a_1 ) = n (a_1q_2-1) - m a_1 q_1 = a_1 (n q_2 - m q_1) - n = a_1 b - n. \end{aligned}$$

Using \(a_1\le q_1\), this shows that \(a<b\). We conclude that \(\Psi \) is surjective.

Finally, we transform the system (4.3) into the system (4.2) by changing the variables slightly by means of the following map:

This is easily checked to be a bijection; we omit the details. \(\square \)

4.3 Proof of Theorem 2.2

In view of Lemma 4.4, it suffices to count the number of solutions of the system

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} \gcd (p,q) = 1, &{} p,q\ge 1, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2, \\ 2 \le n q + kp \le Q, &{} 1\le k<n , \end{array} \right. \end{aligned}$$
(4.6)

with an error term of size \({O( Q^2 )}\). The reader may notice the similarity between the system (4.6) and the system [30, Eq. (42)]: they are almost identical, up to the additional constraints concerning coprimality and modular inversion. Set \(U=Q^{1/2}\) and consider the following five cases:

  • \(p \le q \le U\); (‘Case 1’)

  • \(p \le q\), \(U < q\); (‘Case 2’)

  • \(q < p \le U\); (‘Case 3’)

  • \(q < p\), \(U < p\), \(n \le U\); (‘Case 4’)

  • \(q < p\), \(U < p\), \(U < n\). (‘Case 5’)

Those cases are exactly the five cases appearing in [30]. The following proposition provides us the asymptotic number of solutions for each single case.

Proposition 4.5

Suppose that \(1\le i\le 5\) and let \(R_i(U)\) denote the number of solutions to the system (4.6) subject to the additional constraint that ‘Case i’ be satisfied. Then we have

  1. 1.

    \({\displaystyle R_1(U)=\frac{\log 2}{4\zeta (2)}U^4\log U+O( U^4 )}\),

  2. 2.

    \({\displaystyle R_2(U)=\frac{\log 2}{4\zeta (2)}U^4\log U+O( U^4 )}\),

  3. 3.

    \({\displaystyle R_3(U)=\frac{U^4( \log U )^2}{8\zeta (2)}+\frac{U^4\log U}{4\zeta (2)}\biggl ( {\gamma }-\frac{\zeta '(2)}{\zeta (2)}+\frac{3\zeta (2)}{4}-\log 2 \biggr )+O( U^4 )}\),

  4. 4.

    \({\displaystyle R_4(U)=\frac{U^4( \log U )^2}{8\zeta (2)}+\frac{U^4\log U}{4\zeta (2)}(\gamma -\log 2)+O( U^4 )}\),

  5. 5.

    \({\displaystyle R_5(U)=\dfrac{U^4}{4\zeta (2)}( \log U )^2+\dfrac{U^2}{2\zeta (2)}\biggl ( \gamma - \dfrac{\zeta '(2)}{2\zeta (2)}-\dfrac{3}{2}{+\,\dfrac{3\zeta (2)}{8}} \biggr )\log U+O( U^4 )}\).

The proof of Proposition 4.5 is the most technical part of the paper. We postpone it until Sect. 5.

Assuming the conclusion of Proposition 4.5 for the moment, we are now in a position to finish the proof of Theorem 2.2. Indeed, by the above, we find that the number of solutions of the system (4.6) is equal to

$$\begin{aligned} {\dfrac{U^4}{2\zeta (2)}( \log U )^2+\dfrac{U^4}{2\zeta (2)}\biggl ( 2\gamma -\dfrac{ \zeta '(2)}{\zeta (2)}-\dfrac{3}{2}{+\,\dfrac{3\zeta (2)}{4}} \biggr )\log U+O( U^4 ).} \end{aligned}$$

Substituting \(U=Q^{1/2}\), we conclude for real numbers \(Q>0\) which are not squares that

$$\begin{aligned} {N_0(Q) =\dfrac{Q^2}{8\zeta (2)}( \log Q )^2+\dfrac{Q^2}{4\zeta (2)}\biggl ( 2\gamma -\dfrac{\zeta '(2)}{\zeta (2)}-\dfrac{3}{2}{+\dfrac{3\zeta (2)}{4}} \biggr )\log Q+O( Q^2 ),} \end{aligned}$$
(4.7)

where \(N_0(Q)\) is the quantity described in Lemma 4.3. To obtain the same result in case Q is a square, it suffices to notice that the asymptotic formula for \(N_0(Q+1/2)\) matches (4.7) up to an error of order \(O(Q\log Q)\). To finish the proof, we still have to restrict to the set \({\mathscr {F}}_0(Q)\). To this end, notice that by Möbius inversion we have

$$\begin{aligned} \sum _{x\in {\mathscr {F}}_0(Q)} \ell (x)&=\mathop {\sum _{b\le Q}\sum \limits _{a<b/2}}_{\gcd (a,b)=1} \ell \biggl ( \frac{a}{b} \biggr ) =\sum \limits _{d\le Q}\mu (d)\mathop {\sum _{b\le Q/d}\sum \limits _{a<b/2}} \ell \biggl ( \frac{a}{b} \biggr ) \\&=\sum \limits _{d\le Q}\mu (d)N_0\biggl ( \dfrac{Q}{d} \biggr ). \end{aligned}$$

Hence, we deduce from Lemma A.3 and (4.7) that

$$\begin{aligned} {\sum _{x\in {\mathscr {F}}_0(Q)} \ell (x)=\dfrac{Q^2(\log Q)^2}{8\zeta (2)^2}+\dfrac{Q^2\log Q}{4\zeta (2)^2}\biggl ( 2\gamma -\dfrac{3}{2}-2\dfrac{\zeta '(2)}{\zeta (2)}+\dfrac{3\zeta (2)}{4} \biggr )+O( Q^2 ).} \end{aligned}$$

This concludes the proof of Theorem 2.2. \(\square \)

5 Proof of Proposition 4.5

As mentioned in Sect. 4.3, we count the solutions of (4.6) in five different cases which are exactly those considered by Zhabitskaya with the additional restrictions on coprimality and modular inversion. Therefore, in what follows we often refer to the proof of [30, Theorem 2] as it contains several estimates which we employ directly here to simplify our exposition.

5.1 Case 1

We count the number of solutions \(R_1(U)\) of

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} \gcd (p,q) = 1, &{} 1\le p\le q\le U, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2,\\ 2 \le n q + kp \le U^2, &{} 1\le k<n. \end{array} \right. \end{aligned}$$
(5.1)

If p and q are fixed, then the number of solutions of the above system with respect to the various \( 1\le k<n\) has been shown in [30, (45)] to be equal to

$$\begin{aligned} \Sigma (p,q) :=\frac{U^4}{2q(p+q)}+E(U,p,q), \end{aligned}$$

where E(Upq) is given explicitly in [30, (45)]. Thus, the number of solutions of (5.1) is equal to

$$\begin{aligned} \mathop {\sum _{q\le U}\sum _{p\le q}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\Sigma (p,q) =\frac{U^4}{2}\mathop {\sum _{p\le U}\sum _{p\le q\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{q(p+q)} +O\biggl ( \mathop {\sum _{q\le U}\sum _{p\le q}}E(U,p,q) \biggr ). \end{aligned}$$
(5.2)

The error term above has been proved in [30, (45)–(47)] to be \({O( U^3 )}\). It remains to compute the first double sum in the right-hand side of (5.2). We deal with the inner sum over q first. To this end, we set

$$\begin{aligned} f(x)=\frac{1}{x(p+x)},\quad g(x)=\frac{\varphi (p)}{2p}(x-p)\quad \text {and}\quad M(x)=\frac{x}{p^{1/2-\epsilon }}. \end{aligned}$$

Then Lemmas A.2 and A.1 yield that

$$\begin{aligned}&\mathop {\sum _{p\le q\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{q(p+q)} \begin{aligned}&\quad =\frac{\varphi (p)}{2p}\int _p^U\frac{\mathop {\mathrm {d}x}}{x^2+xp} {} \\&\qquad \qquad + O_\epsilon \biggl ( \frac{1}{p^{3/2-\epsilon }}+ {\int _p^U\frac{x(2x+p)}{p^{1/2-\epsilon }(x^2+xp)^2}\mathop {\mathrm {d}x}} \biggr ) \\&\quad =\frac{\varphi (p)}{2p^2}{\int _{p}^U\biggl ( \frac{1}{x}-\frac{1}{x+p} \biggr )\mathop {\mathrm {d}x}} + O_\epsilon \biggl ( p^{-3/2+\epsilon } \biggr ) \\&\quad =\frac{\varphi (p)}{2p^2}\log 2+O( U^{-1} ) + O_\epsilon \biggl ( p^{-3/2+\epsilon } \biggr ). \end{aligned} \end{aligned}$$

We now take \(\epsilon =1/3\) (any \(\epsilon <1/2\) would do) and sum the above terms over \(p\le U\). Our choice of \(\epsilon \) ensures that the sum over the error terms remains bounded. In view of Lemma A.5 (3), we conclude that

$$\begin{aligned} \mathop {\sum _{p\le U}\sum _{p\le q\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{q(q+p)}=\frac{\log 2}{2}\sum _{p\le U}\frac{\varphi (p)}{p^2}+O(1)=\frac{\log 2}{2\zeta (2)}\log U+O(1). \end{aligned}$$
(5.3)

For later use, observe also that the relation

$$\begin{aligned} \mathop {\sum _{q< U}\sum _{q< p\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{q}(p)>q/2 \end{array}}\frac{1}{p(q+p)}=\frac{\log 2}{2\zeta (2)}\log U+O(1) \end{aligned}$$
(5.4)

can be derived in the same way as relation (5.3) was. Finally, upon combining (5.2) with (5.3), we conclude that

$$\begin{aligned} {R_1( U )=\frac{\log 2}{4\zeta (2)}U^4\log U+O( U^4 ).} \end{aligned}$$

5.2 Case 2

We count the number of solutions \(R_2(U)\) of

$$\begin{aligned} \left\{ \begin{array}{@{}lll@{}} \gcd (p,q) = 1, &{} 1\le p\le q,&{}U<q, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2,\\ 2 \le n q + kp \le U^2, &{} 1\le k<n. \end{array} \right. \end{aligned}$$
(5.5)

In this case the inequalities \( n\le {U^2}/{q}<U\) hold as well.

Let and fix k and n. If \(n+k\le U\), then the domain of solutions of the above system can be expressed as the latticeFootnote 5

$$\begin{aligned} S_1(n,k)=\left\{ (p,q)\in {\mathcal {C}}:1\le p\le \frac{U^2}{n+k},\, U<q\le \frac{U^2-kp}{n},\,{{\,\mathrm{inv}\,}}_p(q)\le \frac{p}{2}\right\} \end{aligned}$$

without the points of the lattice

$$\begin{aligned} S_2(n,k)=\left\{ (p,q)\in {\mathcal {C}}:U<p\le \frac{U^2}{n+k},\, U<q\le p,\,{{\,\mathrm{inv}\,}}_p(q)\le \frac{p}{2}\right\} . \end{aligned}$$

The number of integer points in \(S_1(n,k)\) is equal to

$$\begin{aligned} \Sigma _1(n,k) :=\sum _{p\le U^2/(n+k)} A_p\biggl ( U,\frac{U^2-kp}{n} \biggr ), \end{aligned}$$

where \(A_p(y,x)\) is defined in Lemma A.1. Therefore, it follows that

$$\begin{aligned} \begin{aligned} \Sigma _1(n,k)&=\sum _{p\le U^2/(n+k)}\frac{\varphi (p)}{2p}\biggl ( \frac{U^2}{n}-U-p\frac{k}{n} \biggr ) +{} \\&\qquad + \sum _{p\le U^2/(n+k)}O_\epsilon \biggl ( \frac{U^2-kp-nU+np}{np^{1/2-\epsilon }} \biggr )\\&=:S_{11}+S_{12}. \end{aligned} \end{aligned}$$
(5.6)

Regarding the first sum, Lemma A.5 (1)–(2) and inequalities \(k<n<U\) yield that

$$\begin{aligned} \begin{aligned} S_{11}&=\biggl ( \frac{U^2}{n}-U \biggr ) \biggl ( \frac{U^2}{2\zeta (2)(n+k)}+O \biggl ( \log \frac{U^2}{n+k} \biggr ) \biggr ){}\\&\quad -\frac{k}{n}\biggl ( \frac{U^4}{4\zeta (2)(n+k)^2}+ O\biggl ( \frac{U^2}{n+k}\log \frac{U^2}{n+k} \biggr ) \biggr )\\&=\frac{U^4}{2\zeta (2)n(n+k)}-\frac{U^3}{2\zeta (2)(n+k)}-\frac{kU^4}{4\zeta (2)n(n+k)^2}+O \biggl ( \frac{U^2}{n}\log \frac{U^2}{n+k} \biggr )\\&=\frac{U^4}{4\zeta (2)n(n+k)}+\frac{U^4}{4\zeta (2)(n+k)^2} -\frac{U^3}{2\zeta (2)(n+k)}+O\biggl ( \frac{U^2}{n} \log \frac{U^2}{n+k} \biggr ). \end{aligned} \end{aligned}$$

For the sum \(S_{12}\) over the error terms, we estimate

$$\begin{aligned} {S_{12} \ll _\epsilon \frac{U^2-nU}{n}\biggl ( \frac{U^2}{n+k} \biggr )^ {1/2+\epsilon }+\frac{n-k}{n} \biggl ( \frac{U^2}{n+k} \biggr )^{3/2+\epsilon }\ll _ \epsilon \frac{U^{3+2\epsilon }}{n(n+k)^{1/2+\epsilon }}.} \end{aligned}$$

We work similarly for the number of integer points in \(S_2(n,k)\):

$$\begin{aligned} \Sigma _2(n,k)&=\sum _{U<p\le U^2/(n+k)}A_p( U,p ) \\&=\sum _{U<p\le U^2/(n+k)}\biggl ( \frac{\varphi (p)}{2p}(p-U)+ O_\epsilon \biggl ( \frac{2p+U}{p^{1/2-\epsilon }} \biggr ) \biggr ). \end{aligned}$$

Once more, Lemma A.5 (1)–(2) and inequalities \(k<n<U\) yield that

$$\begin{aligned}&\sum _{U<p\le U^2/(n+k)}\frac{\varphi (p)}{2p}(p-U) =\frac{1}{4\zeta (2)}\biggl ( \frac{U^4}{(n+k)^2}-U^2 \biggr )+ O\biggl ( \frac{U^2}{n+k}\log \frac{U^2}{n+k} \biggr ){}\\&\quad -\frac{U}{2\zeta (2)}\biggl ( \frac{U^2}{n+k}- U+O\biggl ( \log \frac{U^2}{n+k} \biggr ) \biggr )\\&\quad =\frac{U^4}{4\zeta (2)(n+k)^2}-\frac{U^3}{2\zeta (2)(n+k)}+O \biggl ( U^2+\frac{U^2}{n}\log \frac{U^2}{n+k} \biggr ), \end{aligned}$$

while for the sum of the error terms we obtain that

$$\begin{aligned} \sum _{U<p\le U^2/(n+k)}O_\epsilon \biggl ( \frac{2p+U}{p^{1/2-\epsilon }} \biggr ) \ll _\epsilon \sum _{U<p\le U^2/(n+k)}p^{1/2+\epsilon } \ll _\epsilon \frac{U^{3+2\epsilon }}{(n+k)^{3/2+\epsilon }}.\qquad \end{aligned}$$
(5.7)

In view of (5.6)–(5.7) and Lemma A.4 (1), we conclude that the number of solutions of the system (5.5) for pairs \((n,k)\in \mathbb {N}^2\) such that \(1\le k<n\) and \(n+k\le U\), is equal to

$$\begin{aligned}&\mathop {\sum _{n<U}\sum _{k<n}}_{n+k\le U}( \Sigma _1(n,k)-\Sigma _2(n,k) )\\&\quad =\frac{U^4}{4\zeta (2)}\mathop {\sum _{n<U}\sum _ {k<n}}_{n+k\le U}\frac{1}{n(n+k)} + \mathop {\sum _{n<U}\sum _{k<n}}_{n+k\le U}\biggl [O( U^2 )+O_\epsilon \biggl ( \frac{U^{3+2\epsilon }}{nk^{1/2+\epsilon }} \biggr )\biggr ]\\&\quad =\frac{\log 2}{4\zeta (2)}U^4\log U+O( U^4 )+O_\epsilon \biggl ( U^{7/2+2\epsilon } \biggr ). \end{aligned}$$

Now we consider the pairs \((n,k)\in \mathbb {N}^2\) for which \(1\le k<n\) and \(n+k>U\). In that case the number of solutions of the system (5.5) is smaller than the number of solutions of the same system without the restrictions on coprimality and modular inversion. This number has been computed in [30, (54)–(56)] to be \({O( U^4 )}\). Therefore, by fixing \(\epsilon \in (0,1/4)\), we obtain that

$$\begin{aligned} {R_2(U)=\frac{\log 2}{4\zeta (2)}U^4\log U+O( U^4 ).} \end{aligned}$$

5.3 Case 3

We count the number of solutions \(R_3(U)\) of

$$\begin{aligned} \left\{ \begin{array}{@{}ll@{}} \gcd (p,q) = 1, &{} 1\le q<p\le U, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2,\\ 2 \le n q + kp \le U^2, &{} 1\le k<n. \end{array} \right. \end{aligned}$$

Similar as in Case 1 (see also [30, (58)–(60)]), the number of solutions of the above system is equal to

$$\begin{aligned} {\frac{U^4}{2}\mathop {\sum _{p\le U}\sum _{ q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{q(p+q)}+O( U^3\log U ).} \end{aligned}$$
(5.8)

It remains to compute the double sum

$$\begin{aligned} \begin{aligned} \mathop {\sum _{p\le U}\sum _{ q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{q(p+q)}&=\mathop {\sum _{p\le U}\sum _{ q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{pq}-\mathop {\sum _{p\le U}\sum _{q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{p(q+p)}\\&=\mathop {\sum _{p\le U}\sum _{ p^{1/2}\le q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{pq}+\mathop {\sum _{p\le U}\sum _{q<p^{1/2}}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{pq}-\mathop {\sum _{p\le U}\sum _{q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{p(q+p)}\\&=:S_1+S_2-S_3. \end{aligned} \end{aligned}$$
(5.9)

In view of Lemma 4.1 and our remark (5.4), we have that

$$\begin{aligned} S_3 =\mathop {\sum _{p\le U}\sum _{q<p}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{p(q+p)} =\mathop {\sum _{q< U}\sum _{q< p\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{q}(p)>q/2 \end{array}}\frac{1}{p(q+p)} =\frac{\log 2}{2\zeta (2)}\log U+O(1). \end{aligned}$$
(5.10)

Interchanging the sums in \(S_1\) and applying Lemma 4.1 yield that

$$\begin{aligned} S_1=\sum _{q< U}\frac{1}{q}\mathop {\sum _{ q<p\le V_q}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{q}(p)> q/2 \end{array}}\frac{1}{p}, \end{aligned}$$

where \(V_q :=\min \{U,q^{2}\}\). If we set

$$\begin{aligned} f(x)=\frac{1}{x},\quad g(x)=\frac{\varphi (q)}{2q}(x-q)\quad \text {and}\quad M(x)=\frac{x}{q^{1/2-\epsilon }}, \end{aligned}$$

then it follows from Lemmas A.1 and A.2 that

$$\begin{aligned} \mathop {\sum _{q<p\le V_q}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{q}(p)> q/2 \end{array}}\frac{1}{p} \begin{aligned}&= \frac{\varphi (q)}{2q}\int _q^{V_q}\frac{\mathop {\mathrm {d}x}}{x} + O_\epsilon \biggl ( q^{-1/2+\epsilon }+\int _q^{V_q}\frac{\mathop {\mathrm {d}x}}{xq^{1/2-\epsilon }} \biggr )\\&= \frac{\varphi (q)}{2q}\log \frac{V_q}{q} + O_{\epsilon }\biggl ( q^{-1/2+2\epsilon } \biggr ). \end{aligned} \end{aligned}$$

Hence,

$$\begin{aligned} {S_1 = \sum _{q<U^{1/2}}\frac{\varphi (q)}{2q^2}\log q+\sum _{U^{1/2}\le q<U}\frac{\varphi (q)}{2q^2}( \log {U}-\log {q} )+\sum _{q<U}O_{\epsilon } \biggl ( q^{-3/2+2\epsilon } \biggr ).} \end{aligned}$$

We now take \(\epsilon =1/5\), so that the last sum on the right hand side converges if U is replaced by \(\infty \) (any \(\epsilon <1/4\) would do). Therefore, in view of Lemma A.5 (3)–(4), we obtain that

$$\begin{aligned} {\begin{aligned} S_1&=\frac{( \log U )^2}{16\zeta (2)}+ \frac{( \log U )^2}{4\zeta (2)}+O\biggl ( \frac{( \log U )^2}{U} \biggr )- \frac{3( \log U )^2}{16\zeta (2)}+O(1)\\&=\frac{( \log U )^2}{8\zeta (2)}+O(1). \end{aligned}} \end{aligned}$$
(5.11)

Lastly, we proceed with the computation of \(S_2\) where the bias in the \( \mathrm {EA}^{(\mathrm {div})}_{(\text {by-excess})} \) makes its appearance for the first time. Interchanging the sums in \(S_2\) and applying Lemma 4.1 yield that

$$\begin{aligned} \begin{aligned} S_2&= \mathop {\sum _{p\le U}\sum _{q<p^{1/2}}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{p}(q)\le p/2 \end{array}}\frac{1}{pq} =\sum _{q< U^{1/2}}\frac{1}{q}\mathop {\sum _{q^{2}<p\le U}}_{\begin{array}{c} \gcd (p,q)=1\\ {{\,\mathrm{inv}\,}}_{q}(p)> q/2 \end{array}}\frac{1}{p} \\&=\sum _{q< U^{1/2}}\frac{1}{q}\mathop {\sum _{q/2<b\le q}}_{\gcd (b,q)=1}\mathop {\sum _{ q^{2}<p\le U}}_{p\equiv {{\,\mathrm{inv}\,}}_{q}(b)\bmod q} \!\!\! \frac{1}{p}. \end{aligned} \end{aligned}$$
(5.12)

Since

for any coprime integers \(1\le b\le q\), we know from Lemma A.2 that

$$\begin{aligned} {\mathop {\sum _{q^2<p\le U}}_{p\equiv {{\,\mathrm{inv}\,}}_{q}(b)\bmod q}\frac{1}{p}=\frac{1}{q}\log \frac{U}{q^{2}}+O( q^{-2} ).} \end{aligned}$$

Inserting this to (5.12) yields that

$$\begin{aligned} {\begin{aligned} S_{2}&=\sum _{q< U^{1/2}}\frac{1}{q}\mathop {\sum _{q/2<b\le q}}_{\gcd (b,q)=1}\biggl [\frac{1}{q}\log \frac{U}{q^2}+O( q^{-2} )\biggr ] \\&=\sum _{q< U^{1/2}}\biggl [\frac{\delta ^+(q)}{q^2}\log \frac{U}{q^{2}}+O\biggl ( \frac{\delta ^+(q)}{q^3} \biggr )\biggr ], \end{aligned}} \end{aligned}$$
(5.13)

where \(\delta ^+(q)\) is defined in Lemma 4.2.

It is clear from relation (5.13) and Lemma 4.2 where the bias occurs. In the case we are considering (for fractions less than 1/2), the terms which correspond to \(q=1\) and \(q=2\) come with weight 1 and 0, while in the complementary case (for fractions greater than 1/2) where the counting function \(\delta ^+\) is replaced by \(\delta ^-\), they come with weight 0 and 1/2, respectively.

Now in view of Lemma 4.2, Lemma A.5 (3)–(4) we have that

$$\begin{aligned} {\begin{aligned} S_2&=\log U+\sum _{ 3\le q< U^{1/2}}{\frac{\varphi (q)}{2q^2}\log \frac{U}{q^{2}}}+O(1)\\&={\frac{1}{2}\log U-\frac{1}{8}\log U}+\sum _{ q< U^{1/2}}\frac{\varphi (q)}{2q^2}( \log {U}-2\log {q} )+O(1)\\&=\frac{3}{8}\log U+\frac{( \log U )^2}{8\zeta (2)}+\frac{\log U}{2\zeta (2)}\biggl ( \gamma -\frac{\zeta '(2)}{\zeta (2)} \biggr )+O(1). \end{aligned}} \end{aligned}$$
(5.14)

Finally, we deduce from (5.8), (5.9), (5.10), (5.11) and (5.14) that

$$\begin{aligned} {R_3(U) =\frac{U^4( \log U )^2}{8\zeta (2)}+\frac{U^4\log U}{4\zeta (2)}\biggl ( {\gamma }-\frac{\zeta '(2)}{\zeta (2)}+\frac{3\zeta (2)}{4}-\log 2 \biggr )+O( U^4 ).} \end{aligned}$$

5.4 Case 4

We count the number of solutions \(R_4(U)\) of

$$\begin{aligned} \left\{ \begin{array}{@{}lll@{}} \gcd (p,q) = 1, &{} 1\le q<p,&{} U<p, \\ {{\,\mathrm{inv}\,}}_p(q)\le p/2,\\ 2 \le n q + kp \le U^2, &{} 1\le k<n\le U. \end{array} \right. \end{aligned}$$
(5.15)

Similar as in Case 2, we fix k and n and count the number of the above system, when \(n+k\le U\) and when \(n+k> U\).

If \(n+k\le U\), then the domain of solutions of (5.15) can be expressed as the union of the latticesFootnote 6

$$\begin{aligned} S_1(n,k) =\left\{ (p,q)\in {\mathcal {C}}:U< p\le \frac{U^2}{n+k},\,1\le q\le p,\,{{\,\mathrm{inv}\,}}_p(q)\le \frac{p}{2}\right\} \end{aligned}$$

and

$$\begin{aligned} S_2(n,k)&=\left\{ (p,q)\in {\mathcal {C}}:\frac{U^2}{n+k}<p\le \frac{U^2}{k},\,1\le q\le \frac{U^2-kp}{n},\,{{\,\mathrm{inv}\,}}_p(q)\le \frac{p}{2}\right\} \\&=\left\{ (p,q)\in {\mathcal {C}}:1\le q\le \frac{U^2}{n+k}-\theta ,\, \frac{U^2}{n+k}<p\le \frac{U^2-nq}{k},\,{{\,\mathrm{inv}\,}}_q(p)>\frac{q}{2}\right\} , \end{aligned}$$

where we have employed above Lemma 4.1 and have introduced a parameter \(\theta \in [0,1]\) which may vary. The number of integer points in \(S_1(n,k)\) is equal to

$$\begin{aligned} \Sigma _1(n,k) :=\sum _{U<p\le U^2/(n+k)}\mathop {\sum _{b\le p/2}}_{\gcd (b,p)=1}\mathop {\sum _{ q\le p}}_{q\equiv {{\,\mathrm{inv}\,}}_{p}(b)\bmod p}1 =\sum _{U<p\le U^2/(n+k)}\frac{\varphi (p)}{2}. \end{aligned}$$

It follows now from Lemma A.5 (1) that

$$\begin{aligned} \begin{aligned} \Sigma _1(n,k)&=\frac{1}{4\zeta (2)}\biggl ( \biggl ( \frac{U^2}{n+k} \biggr ) ^2-U^2 \biggr )+O\biggl ( \frac{U^2}{n+k}\log \frac{U^2}{n+k} \biggr )\\&=\frac{U^4}{4\zeta (2)(n+k)^2}+O\biggl ( U^2+\frac{U^2}{n+k} \log \frac{U^2}{n+k} \biggr ). \end{aligned} \end{aligned}$$
(5.16)

The number of integer points in \(S_2(n,k)\) is equal to

$$\begin{aligned} \Sigma _2(n,k) :=\sum _{q\le {U^2}/(n+k)-\theta } B_q\biggl ( \frac{U^2}{n+k},\frac{U^2-nq}{k} \biggr ), \end{aligned}$$

where \(B_q(y,x)\) is defined in Lemma A.1. Upon applying said lemma, we infer that

$$\begin{aligned} \Sigma _{2}(n,k) = S_{21}+O_\epsilon (S_{22}), \end{aligned}$$

where

$$\begin{aligned} S_{21}= & {} \sum _{q\le {U^2}/(n+k)-\theta } \frac{\varphi (q)}{2q} \biggl ( \frac{nU^2}{k(n+k)}-\frac{nq}{k} \biggr ), \\ S_{22}= & {} \sum _{q\le {U^2}/(n+k)-\theta } \biggl ( \frac{nU^2}{k(n+k)}-\frac{nq}{k}+q \biggr )q^{-1/2+\epsilon }. \end{aligned}$$

From Lemma A.5 (1)–(2) and inequalities \(k<n<n+k\le U\) we obtain that

$$\begin{aligned} S_{21}&=\frac{nU^2}{2k(n+k)\zeta (2)} \biggl ( \frac{U^2}{n+k}-\theta +O\biggl ( \log \frac{U^2}{n+k} \biggr ) \biggr )+{}\\&\quad -\frac{n}{4k\zeta (2)}\biggl ( {\biggl ( \frac{U^2}{n+k}-\theta \biggr )^2+O\biggl ( \frac{U^2}{n+k}\log \frac{U^2}{n+k} \biggr )} \biggr )\\&=\frac{nU^4}{4\zeta (2)k(n+k)^2}+O \biggl ( \frac{nU^2}{k(n+k)}\log \frac{U^2}{n+k} \biggr ). \end{aligned}$$

For the sum over the error terms we estimate

$$\begin{aligned} S_{22}&\ll _\epsilon \frac{nU^2}{k(n+k)}\biggl ( \frac{U^2}{n+k} \biggr )^ {1/2+\epsilon }+\frac{n-k}{k}\biggl ( \frac{U^2}{n+k} \biggr )^{3/2+\epsilon }\nonumber \\&\quad \ll _\epsilon \frac{nU^{3+2\epsilon }}{k(n+k)^{3/2+\epsilon }}. \end{aligned}$$
(5.17)

In view of (5.16)–(5.17) and Lemma A.4 (2), we deduce that the number of solutions of the system (5.15) for pairs \((n,k)\in \mathbb {N}^2\) such that \(1\le k<n\) and \(n+k\le U\), is equal to

$$\begin{aligned}&\mathop {\sum _{n<U}\sum _{k<n}}_{n+k\le U} ( \Sigma _1(n,k)+\Sigma _2(n,k) ) \\&\quad =\frac{U^4}{4\zeta (2)}\mathop {\sum _{n<U}\sum _{k<n}}_{n+k\le U}\frac{1}{k(n+k)}+\mathop {\sum _{n<U}\sum _{k<n}}_{n+k\le U} \biggl [O( U^2 )+O_\epsilon \biggl ( \frac{U^{3+2\epsilon }}{kn^{1/2+\epsilon }} \biggr )\biggr ]\\&\quad =\frac{U^4( \log U )^2}{8\zeta (2)}+\frac{U^4\log U(\gamma -\log 2)}{4\zeta (2)}+O( U^4 )+ O_\epsilon \biggl ( U^{7/2+2\epsilon } \biggr ). \end{aligned}$$

Now we consider the pairs \((n,k)\in \mathbb {N}^2\) for which \(1\le k<n\) and \(n+k>U\). In that case the number of solutions of the system (5.15) is smaller than the number of solutions of the same system without the restrictions on coprimality and modular inversion. This number has been computed in [30, (64)–(65)] to be \({O( U^4 )}\). Therefore, by fixing \(\epsilon \in (0,1/4)\), we see that

$$\begin{aligned} R_4(U) =\frac{U^4( \log U )^2}{8\zeta (2)}+\frac{U^4\log U}{4\zeta (2)}(\gamma -\log 2)+O( U^4 ). \end{aligned}$$

5.5 Case 5

We now count the number of solutions \(R_5(U)\). Employing Lemma 4.1, we find that this is the same as counting the number of solutions of the system

$$\begin{aligned} \left\{ \begin{array}{@{}lll@{}} \gcd (p,q) = 1, &{} 1\le q<p,&{} U<p, \\ {{\,\mathrm{inv}\,}}_q(p)> q/2,\\ 2 \le n q + kp \le U^2, &{} 1\le k<n,&{}U<n. \end{array} \right. \end{aligned}$$
(5.18)

Notice that the set of solutions of the above system is non-empty if, and only if, \(k+q<U\).

For fixed k and q the number of solutions of (5.18) with respect to the various n and p is equal to

$$\begin{aligned} \Sigma (k,q)&=\sum _{U<n\le ( U^2-k\lceil U\rceil )/q}\mathop {\sum _{q/2<b\le q}}_{\gcd (b,q)=1}\mathop {\sum _{ U<p\le ( U^2-nq )/k}}_{p\equiv {{\,\mathrm{inv}\,}}_{q}(b)\bmod q}1\\&=\sum _{U<n\le ( U^2-k\lceil U\rceil )/q}\mathop {\sum _{q/2<b\le q}}_{\gcd (b,q)=1} \biggl ( \frac{1}{q}\biggl ( \frac{U^2-nq}{k}-U \biggr )+O(1) \biggr )\\&=\sum _{U<n\le ( U^2-k\lceil U\rceil )/q}\biggl ( \frac{\delta ^+(q)}{q} \biggl ( \frac{U^2-nq}{k}-U \biggr )+O\biggl ( \frac{\delta ^+(q)}{q} \biggr ) \biggr ), \end{aligned}$$

where \(\lceil x\rceil :=\lfloor x\rfloor +1\) is the ceiling function. From Lemma 4.2 and \(k<U\) we deduce that

$$\begin{aligned} \Sigma (k,1)&=\sum _{U<n\le U^2-k\lceil U\rceil } \biggl ( \frac{U^2}{k}-U-\frac{n}{k} \biggr )+O( U^2 )\\&=\biggl ( \frac{U^2}{k}-U \biggr )( U^2-k\lceil U\rceil -\lfloor U\rfloor )+{}\\&\quad -\frac{( U^2-k\lceil U\rceil )^2+U^2-k \lceil U\rceil -{\lceil U \rceil }\lfloor U\rfloor }{2k}+O( U^2 )\\&=\frac{U^4}{2k}+O( U^3 ) \end{aligned}$$

and \(\Sigma (k,2)=0\). Here is another case where the bias in the Euclidean algorithm appears. Lastly, if \(q\ge 3\), then

$$\begin{aligned} \Sigma (k,q)&=\sum _{U<n\le \biggl ( U^2-k\lceil U\rceil \biggr )/q} \frac{\varphi (q)}{2q}\biggl ( \frac{U^2}{k}-U-\frac{nq}{k} \biggr )+ O( U^2 )\\&=\frac{\varphi (q)}{2q}\biggl ( \frac{U^2}{k}-U \biggr ) \biggl ( \frac{U^2-kU+O(k)}{q}-U+O(1) \biggr )+ O( U^2 )+{}\\&\quad -\frac{\varphi (q)}{4k}\biggl ( \biggl ( \frac{U^2-k U+O(k)}{q}+O(1) \biggr )^2- (U+O(1))^2 \biggr ) \end{aligned}$$

and by expanding each of the products we obtain that

$$\begin{aligned} \Sigma (k,q)&=\frac{\varphi (q)}{2q}\biggl ( \frac{U^4}{kq}-\frac{2U^3}{q}- \frac{U^3}{k}+\frac{kU^2}{q}+O( U^2 ) \biggr )+ O( U^2 )+{}\\&\quad -\frac{\varphi (q)}{4k}\biggl ( \frac{U^4-2kU^3+k^2U^2}{q^2}+O \biggl ( \frac{kU^2}{q^2}+\frac{U^2}{q} \biggr )-U^2+O(U) \biggr )\\&=\frac{\varphi (q)U^4}{4kq^2}-\frac{\varphi (q)U^3}{2q^2}- \frac{\varphi (q)U^3}{2qk}+\frac{\varphi (q)kU^2}{4q^2}+ \frac{\varphi (q)U^2}{4k}+O( U^2 ). \end{aligned}$$

Now we sum up over all pairs \((k,q)\in \mathbb {N}^2\) such that \(k+q<U\), which is essentially equal to \(R_5(U)\):

$$\begin{aligned} \begin{aligned} \mathop {\sum _k\sum _q}_{k+q<U}\Sigma (k,q)&=\frac{U^4}{4}\biggl ( \mathop {\sum _k\sum _q} _{k+q<U}\frac{\varphi (q)}{kq^2}+\sum _{k\le U-1}\frac{1}{k} -\sum _{k\le U-2}\frac{1}{4k} \biggr )+O ( U^4 )+{}\\&\quad -\frac{U^3}{2}\mathop {\sum _k\sum _q}_{k+q<U} \biggl ( \frac{\varphi (q)}{q^2}+\frac{\varphi (q)}{qk} \biggr ) +\frac{U^2}{4}\mathop {\sum _k\sum _q}_{k+q<U} \biggl ( \frac{\varphi (q)k}{q^2}+\frac{\varphi (q)}{k} \biggr ). \end{aligned} \end{aligned}$$

Each of the above sums is already given in Lemma A.6, except of the harmonic sums

$$\begin{aligned} \sum _{k\le U-1}\frac{1}{k}-\sum _{k\le U-2}\frac{1}{4k}=\frac{3}{4}\log U+O( 1 ) \end{aligned}$$

which have occurred here, because the quantities \(\Sigma (k,1)\) and \(\Sigma (k,2)\) are not of the form

$$\begin{aligned} \frac{U^2\varphi (q)}{4kq^2}+O( U^3 ),\quad q=1,2, \end{aligned}$$

respectively. Thus, we conclude that

$$\begin{aligned} R_5(U) =\frac{U^4( \log U )^2}{4\zeta (2)}+\frac{U^2\log U}{4\zeta (2)}\biggl ( 2\gamma -\frac{\zeta '(2)}{\zeta (2)}-3+\frac{3\zeta (2)}{4} \biggr )+O( U^4 ). \end{aligned}$$