text:
# 1 The computational task of solving Pell’s equation

## 1 The computational task of solving Pell’s equation

Let $``$, $``$, $``$ and $``$ denote respectively the sets of natural numbers, integers, rational numbers and real numbers. Pell’s equation is

$$x^2dy^2=1$$
(1)
where $`d`$ is a square-free natural number (i.e. not divisible by $`p^2`$ for any prime $`p`$). We wish to find (positive) integer solutions $`(x,y)\times `$.

Pell’s equation has a rich and colourful history spanning almost 2000 years (cf.  and further references therein). Diophantus (c. 250AD) gave solutions for $`d=26`$ and 30 and we read that the English mathematician John Pell (1610-1685) actually had nothing to do with the equation! The appellation “Pell’s equation” is based on a confusion originating with Euler who mis-attributed a solution to Pell which was actually provided by Lord Brouckner in response to a challenge of Fermat.

It is known that Pell’s equation has infinitely many solutions for any square-free $`d`$. An elementary proof of this fact can be found in  chapter 17 §5.

Since $`\sqrt{d}`$ is irrational for any square-free integer $`d`$, we have

$`a+b\sqrt{d}=x+y\sqrt{d}`$ for $`a,b,x,y`$ (or $``$) iff $`a=x`$ and $`b=y`$. (2)
Hence we can uniquely code any solution $`(x,y)`$ as $`x+y\sqrt{d}`$. Correspondingly we will say that $`\sigma `$ is a solution of Pell’s equation if $`\sigma `$ has the form $`\sigma =s+t\sqrt{d}`$ with $`s,t`$ and $`s^2dt^2=1`$.

If $`\xi =x+y\sqrt{d}`$ for $`x,y`$ we introduce the conjugation operation

$$\overline{\xi }=\overline{x+y\sqrt{d}}=xy\sqrt{d}$$

which is well defined by eq. (2). It has the following immediate properties:

$$\overline{\overline{\xi }}=\xi \overline{\xi +\eta }=\overline{\xi }+\overline{\eta }\overline{\xi \eta }=\overline{\xi }\overline{\eta }.$$
(3)
Pell’s equation can be written

$$\xi \overline{\xi }=1$$
(4)
so any solution $`\xi =x+y\sqrt{d}`$ has the property that $`\overline{\xi }=xy\sqrt{d}=\frac{1}{\xi }`$.

###### Proposition 1

If $`\alpha =a+b\sqrt{d}`$ and $`\xi =x+y\sqrt{d}`$ are both solutions of Pell’s equation then so are (i) $`\overline{\alpha }`$ and (ii) $`\alpha \xi =(a+b\sqrt{d})(x+y\sqrt{d})`$ (where we multiply out the RHS and write it as $`s+t\sqrt{d}=(ax+byd)+(ay+bx)\sqrt{d}`$).
In particular $`\alpha ^n=(a+b\sqrt{d})^n`$ is a solution for all $`n`$.

Proof (i) follows immediately from eq. (4) and the definition of conjugation. For (ii) we note that $`\alpha \overline{\alpha }=\xi \overline{\xi }=1`$ so $`(\alpha \xi )(\overline{\alpha \xi })=\alpha \overline{\alpha }\xi \overline{\xi }=1`$ i.e. $`\alpha \xi `$ is a solution. Similarly $`\overline{\alpha ^n}=\overline{\alpha }^n`$ so $`\alpha ^n\overline{\alpha ^n}=(\alpha \overline{\alpha })^n=1`$.

Example For $`d=5`$ we have the solution $`9+4\sqrt{5}`$ (i.e. $`x=9`$ and $`y=4`$). Now we easily check that $`(9+4\sqrt{5})^3=2889+1292\sqrt{5}`$ so $`x=2889`$ and $`y=1292`$ is also a solution. In fact every solution for $`d=5`$ can be generated as a power of $`9+4\sqrt{5}`$ (cf. theorem 1 below).

###### Theorem 1

(Lagrange 1768) Let $`\xi _1=x_1+y_1\sqrt{d}`$ be the least positive solution of Pell’s equation i.e. $`x_1,y_1>0`$ and $`\xi _1`$ is minimum amongst such solutions. Then every positive solution $`(s,t)`$ is obtained as a power of $`\xi _1`$:

$$s+t\sqrt{d}=(x_1+y_1\sqrt{d})^n\text{for some }n\text{.}$$

Proof From proposition 1 we have that $`\xi _1^n`$ is a positive solution for any $`n`$.
Conversely suppose that $`s+t\sqrt{d}`$ is a positive solution not of the form $`\xi _1^n`$ for any $`n`$. Then there exists $`k`$ with

$$\xi _1^k<s+t\sqrt{d}<\xi _1^{k+1}$$

so

$$1<(s+t\sqrt{d})(x_1y_1\sqrt{d})^k<(x_1+y_1\sqrt{d}).$$
(5)
Now write

$$\alpha =a+b\sqrt{d}=(s+t\sqrt{d})(x_1y_1\sqrt{d})^k.$$

By proposition 1 we have $`\alpha \overline{\alpha }=1`$ so $`(a,b)`$ is a solution of Pell’s equation and eq. (5) says

$$1<a+b\sqrt{d}<x_1+y_1\sqrt{d}.$$

To complete the proof we show that $`a>0`$ and $`b>0`$ contradicting the fact that $`\xi `$ was the least positive solution. Since $`ab\sqrt{d}=\frac{1}{a+b\sqrt{d}}`$ we have

$$0<ab\sqrt{d}<1.$$

From $`a+b\sqrt{d}>1`$ and $`ab\sqrt{d}>0`$ we get $`a>\frac{1}{2}`$. Hence $`a1`$.
From $`ab\sqrt{d}<1`$ we get $`b>\frac{a1}{\sqrt{d}}0`$. Hence both $`a`$ and $`b`$ are $`>0`$ and we have our contradiction. Thus every positive solution must be a power of $`\xi _1`$.

The smallest positive solution $`\xi (d)=x_1+y_1\sqrt{d}`$ is called the fundamental solution. Solving Pell’s equation is equivalent to giving the fundamental solution. As a computational task the input is the number $`d`$ with input size $`\mathrm{log}d`$. However the magnitude of $`\xi (d)`$ can be as large as $`\xi (d)O(e^\sqrt{d})`$ so even to write down $`\xi (d)`$ we potentially need $`O(\sqrt{d})`$ digits which is exponentially large in the input size.

Examples of fundamental solutions are given in the following table.

$$\begin{array}{ccc}d& x_1& y_1\\ 2& 3& 1\\ 3& 2& 1\\ 5& 9& 4\\ 13& 649& 180\\ 14& 15& 4\\ 15& 4& 1\\ 29& 9801& 1820\\ 61& 1766319049& 226153980\\ 109& 158070671986249& 15140424455100\\ 2009& 141012534067201& 3146065416960\\ 4009& 3799& 60\\ 6009& \text{131634010632725315892594469510599473884013975 }& \text{1698114661157803451688949237883146576681644 }\\ 6013& 40929908599& 527831340\\ 10209& 130969496245430263159443178775& 1296219513663218157975941956\\ 16383& 128& 1\end{array}$$

(The interested reader can find more examples at

http://www.bioinfo.rpi.edu/$``$zukerm/cgi-bin/dq.html
and note also the remark after theorem 3 below, concerning the equation $`x^2dy^2=1`$). To get around the exponentially large size of the integers $`x_1`$ and $`y_1`$ we introduce the regulator (an irrational number):

$$R=\mathrm{ln}(x_1+y_1\sqrt{d}).$$

Then $`R`$ (the least integer $`R`$) has $`O(\mathrm{log}d)`$ digits.
Our fundamental computational task becomes:
Given $`d`$ (a square-free integer) find the regulator $`R`$ to $`n`$ digits of accuracy in poly$`(\mathrm{log}d,n)`$ time.

The best known classical algorithm has running time $`O(e^{\sqrt{\mathrm{log}d}}\mathrm{poly}(n))`$, which is exponential in the input size.
Actually we can avoid having to consider the accuracy parameter $`n`$ using the following result, stating that it suffices to compute the integer part of $`R`$.

###### Proposition 2

If we are given the closest integer above or below the regulator $`R`$ then there exists a classical algorithm that will compute $`R`$ to $`n`$ digits of accuracy with running time poly$`(n,\mathrm{log}d)`$.

We will give a proof later (in section 9 after the proof of theorem 5).

### 1.1 Approach for the efficient quantum algorithm

Using results from algebraic number theory we will set up a function $`h:A`$ (where the nature of the set $`A`$ will be given later, but for now, think of $`A`$ as being $``$ too) with the following properties.
(a) $`h`$ is computable in polynomial time. More precisely if $`x`$ is a real number which is an integer multiple of $`10^n`$ then the value of $`h(x)`$ can be computed accurate to $`10^n`$ in poly$`(\mathrm{log}d,\mathrm{log}x,n)`$ time.
(b) $`h`$ is periodic on $``$ with (irrational) period $`R`$, the regulator, and $`h`$ is one-to-one within each period.
We then adapt the standard quantum Fourier transform period finding algorithm (that is used in Shor’s algorithm and other hidden subgroup problems) to work in the case of the (not finitely generated) abelian group $``$ and irrational period $`R`$. In fact we will just discretise $`h`$ by taking $`x=k/N`$ for suitably large chosen values of $`N`$, and round off values of $`h(x)`$ too, to get a discrete domain and range. Then we show that the resulting function (which is not quite periodic because of rounding effects in both the domain and range) can give the desired approximations to $`R`$ to increasing accuracy (as $`N`$ is increased).

Thus the ingredients of Hallgren’s algorithm fall into two essentially disjoint parts. The first part constructs the function $`h`$ from the classical mathematics of algebraic number theory and shows that it is efficiently computable. This part has no quantum ingredients. The second part (having the whole quantum content) shows how to generalise the standard quantum period finding algorithm to work on real numbers, to determine an irrational period to any desired accuracy.

### 1.2 Note to the reader

One of the most interesting features of Hallgren’s algorithm is that it expands the applicability of quantum computation into new areas of mathematics viz. fundamental computational problems of algebraic number theory, especially the study of ideals of the algebraic integers in quadratic number fields (cf. later for an explanation of all these terms). In particular we get efficient quantum algorithms for the solution for Pell’s equation, the principal ideal problem and the determination of the class group (and none of these have known classical efficient solutions).

In these notes we assume no prior knowledge of algebraic number theory. After much lucubration we have developed a self contained account of the necessary parts of this theory with all properties and theorems being proved by elementary means. Nevertheless for readers unacquainted with algebraic number theory it may be advisable to skip many of the proofs on initial reading, while focussing on the statements, concepts and terminology.

Our account of algebraic number theory is based primarily on  with further reference to , ,  and . The description of the generalised quantum period finding algorithm and its application to Pell’s equation is just an expanded version of Hallgren’s account in .

## 2 Algebraic integers in a quadratic number field

Let $`d`$ be a square-free positive integer. The quadratic number field $`[\sqrt{d}]`$ is defined to be the set

$$[\sqrt{d}]=\{r_1+r_2\sqrt{d}:r_1,r_2\}.$$

We think of $`[\sqrt{d}]`$ as an extension of the usual rational numbers $``$. (Indeed the irrational $`\sqrt{d}`$ amongst rationals behaves rather like the complex $`i`$ amongst reals). It is clearly closed under the usual arithmetic operations of addition, multiplication and formation of reciprocals e.g. if $`\xi =r_1+r_2\sqrt{d}0`$ then $`\frac{1}{\xi }=\overline{\xi }/(\xi \overline{\xi })=(r_1r_2\sqrt{d})/(r_1^2r_2^2d)`$ which is clearly in $`[\sqrt{d}]`$ (and the denominator is never zero as $`\sqrt{d}`$ is irrational). Also in addition to eq. (3) we have $`\overline{\xi /\eta }=\overline{\xi }/\overline{\eta }`$.

$`\xi [\sqrt{d}]`$ is an algebraic integer if $`\xi `$ is the root of a polynomial with integer coefficients and with leading coefficient 1 i.e. for some $`n`$ we have $`\xi ^n+a_{n1}\xi ^{n1}+\mathrm{}+a_1\xi +a_0=0`$ where $`a_0,a_1,\mathrm{},a_{n1}`$. Let $`𝒪`$ denote the set of all algebraic integers in $`[\sqrt{d}]`$. $`𝒪[\sqrt{d}]`$ is a generalisation of the usual notion of integers $``$ in the following sense.

###### Proposition 3

In $``$ the algebraic integers are just the usual integers $``$.

Proof Suppose that $`\frac{p}{q}`$ (with $`p,q`$ having no common divisors except 1) satisfies a polynomial equation as above:

$$(\frac{p}{q})^n+a_{n1}(\frac{p}{q})^{n1}+\mathrm{}+a_0=0.$$

Then multiplying through by $`q^{n1}`$ shows that $`\frac{p^n}{q}`$ is an integer i.e. $`q`$ must divide $`p`$ so $`q=\pm 1`$.

###### Proposition 4

If $`\alpha _1`$ and $`\alpha _2`$ are algebraic integers in $`[\sqrt{d}]`$ then $`\alpha _1+\alpha _2`$ and $`\alpha _1\alpha _2`$ are also algebraic integers.

Proof Clearly $`\alpha _1+\alpha _2`$ and $`\alpha _1\alpha _2`$ are in $`[\sqrt{d}]`$. Since $`\alpha _1`$ and $`\alpha _2`$ are algebraic integers there are polynomial equations

$$\begin{array}{c}\alpha _1^n+k_{n1}\alpha _1^{n1}+\mathrm{}+k_0=0\hfill \\ \alpha _2^m+l_{m1}\alpha _2^{m1}+\mathrm{}+l_0=0\hfill \\ \text{with }k_i,l_i.\hfill \end{array}$$

Let $`V`$ be the set of all $``$-linear combinations of $`\beta _{ij}=\alpha _1^i\alpha _2^j`$ for $`0i<n`$ and $`0j<m`$ i.e. $`V=\{k_{ij}\beta _{ij}:k_{ij}\}`$. Clearly $`\gamma V`$ implies that $`\alpha _1\gamma V`$ and $`\alpha _2\gamma V`$ (as we can use the above polynomial equations to express $`\alpha _1^n`$ and $`\alpha _2^m`$ in terms of lower powers). Hence $`(\alpha _1+\alpha _2)\gamma `$ and $`(\alpha _1\alpha _2)\gamma `$ are both in $`V`$. Thus by lemma 1 below, $`\alpha _1+\alpha _2`$ and $`\alpha _1\alpha _2`$ are algebraic integers.

###### Lemma 1

Let $`\gamma _1,\mathrm{},\gamma _l`$ be any chosen complex numbers and let $`V=\{k_i\gamma _i:k_i\}`$. Suppose that a complex number $`\alpha `$ has the property that $`\alpha \gamma V`$ for all $`\gamma V`$. Then $`\alpha `$ is an algebraic integer.

Proof $`\alpha \gamma _iV`$ so $`\alpha \gamma _i=a_{ij}\gamma _j`$ for some $`a_{ij}`$. Hence $`0=(a_{ij}\delta _{ij}\alpha )\gamma _j`$ so by standard linear algebra we have $`\mathrm{det}(a_{ij}\delta _{ij}\alpha )=0`$, giving the required polynomial equation with $`\alpha `$ as a root.

###### Proposition 5

$`\xi =r+s\sqrt{d}[\sqrt{d}]`$ is an algebraic integer iff $`2r`$ and $`r^2s^2d`$ are both integers.

Proof Note that $`2r=\xi +\overline{\xi }`$ and $`r^2s^2d=\xi \overline{\xi }`$. Hence if both of these are integers then the polynomial equation $`(x\xi )(x\overline{\xi })=0`$ shows that the root $`\xi `$ is an algebraic integer.
Conversely suppose that $`\xi `$ is an algebraic integer. Then $`\overline{\xi }𝒪`$ too (satisfying the same polynomial equation). Hence by proposition 4 we have that $`2r=\xi +\overline{\xi }`$ and $`r^2s^2d=\xi \overline{\xi }`$ are algebraic integers. But these are both pure rationals so by proposition 3 they must be integers.

Example If $`m,n`$ then $`m+n\sqrt{d}`$ is always an algebraic integer for any $`d`$ (as follows immediately from proposition 5) but for some $`d`$ values there are more algebraic integers e.g. if $`d=5`$ then $`\frac{1+\sqrt{5}}{2}`$ is an algebraic integer (since it is the root of $`x^2x+1=0`$). Below we will explicitly characterise $`𝒪`$ for any $`d`$.

Remark on notations For any $`\alpha ,\beta [\sqrt{d}]`$ we will write
$`[\alpha ]=\{m+n\alpha :m,n\}`$
$`\alpha =\{n\alpha :n\}`$
$`\alpha +\beta =\{n\alpha +m\beta :m,n\}`$
In particular we can write $`[\alpha ]=+\alpha `$.
We will also use the following properties:
(a) $`\alpha =\alpha `$.
(b) If $`a,b`$ then $`a+\frac{b}{2}=a+\frac{b^{}}{2}`$ for any $`b^{}`$ having $`b^{}b\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}a`$.
(c) If $`a,b`$ then Euclid’s algorithm states that the greatest common divisor $`\mathrm{gcd}(a,b)`$ of $`a`$ and $`b`$ can be expressed as $`\mathrm{gcd}(a,b)=ka+lb`$ for some $`k,l`$. Hence $`a+b=\mathrm{gcd}(a,b)`$. (To see this let $`g=\mathrm{gcd}(a,b)`$. Then LHS $``$ RHS as $`a=a^{}g`$ and $`b=b^{}g`$ for $`a^{},b^{}`$, so anything in LHS is a multiple of $`g`$. Conversely RHS $``$ LHS, as by Euclid’s algorithm, every multiple of $`g`$ has the form $`k^{}a+l^{}b`$ with $`k^{},l^{}`$).

###### Theorem 2

In $`[\sqrt{d}]`$ the set of algebraic integers has the form

$$𝒪=\{m+n\omega :m,n\}$$

where
if $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ we can take $`\omega =\frac{1+\sqrt{d}}{2}`$;
if $`d\text{2 or 3}\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ we can take $`\omega =\sqrt{d}`$.
(Since $`d`$ is square free we never have $`d0\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$).

Proof Let $`\xi =r+s\sqrt{d}[\sqrt{d}]`$. Suppose $`\xi 𝒪`$. Then $`2r`$ and $`r^2s^2d`$ so $`4s^2d=(2r)^24(r^2s^2d)`$. Hence any prime $`p>2`$ in the denominator of $`s`$ must have $`p^2`$ dividing $`d`$, which is impossible as $`d`$ is square-free. Hence $`2s`$. Set $`2r=m`$ and $`2s=n`$. Then $`r^2s^2d`$ implies $`m^2dn^20\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$. Recall that any square is congruent to 0 or 1 $`\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ (because $`(2k)^2=4k^20`$ and $`(2k+1)^2=4k^2+4k+11\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$).
If $`d\text{2 or 3}\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$: then $`m^2dn^2m^2+2n^2`$ or $`m^2+n^2\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$. The only way that $`m^2+2n^2`$ or $`m^2+n^2`$ can be divisible by 4 is for both $`m`$ and $`n`$ to be even, and this is the case iff $`r`$ and $`s`$ are integers. Hence if $`d\text{2 or 3}\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then any algebraic integer has the form $`m+n\sqrt{d}`$ for $`m,n`$. Conversely by proposition 5 any expression of this form is an algebraic integer.
If $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$: then $`m^2dn^2m^2n^2\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ and this can be divisible by 4 iff $`m`$ and $`n`$ are both even or both odd. Thus any algebraic integer has the form $`\frac{m+n\sqrt{d}}{2}`$ with $`m,n`$ both even or both odd. Conversely any number $`\xi `$ of this form has $`\xi +\overline{\xi }`$ and $`\xi \overline{\xi }=\frac{m^2dn^2}{4}`$ since $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$. Thus by proposition 5 $`\xi `$ is an algebraic integer. Finally writing

$$\frac{m+n\sqrt{d}}{2}=\frac{m+n}{2}+n(\frac{1+\sqrt{d}}{2})$$

with $`\frac{m+n}{2}`$ (as $`m,n`$ have the same parity) we see that

$$\{\frac{m+n\sqrt{d}}{2}:m,n\text{ are both even or odd}\}=\{k+l(\frac{1+\sqrt{d}}{2}):k,l\}\text{ }$$

Note that 1 and $`\omega `$ are linearly independent over $``$ i.e. $`r_11+r_2\omega =0`$ for $`r_1,r_2`$ iff $`r_1=r_2=0`$. Thus we see that $`𝒪`$ behaves like a 2 dimensional “vector space” where 1 and $`\omega `$ are basis vectors but the coefficients for linear combinations must be integers. Such a structure is called a $``$-module. Although we are drawing an analogy with vector spaces it is interesting to note that actually, the notion of $``$-module is the same as the notion of abelian group: clearly any $``$-module as above is an abelian group under addition. Conversely if $`(G,+)`$ is any abelian group then writing $`g+g+\mathrm{}+g`$ ($`n`$ times) as $`(n)g`$ and the inverse $`g`$ of $`g`$ as $`(1)g`$, then $`G`$ can be viewed naturally as a $``$-module i.e. $`G`$ is the same as the set of all $``$-linear combinations of elements of $`G`$.

A pair of elements $`\alpha ,\beta 𝒪`$ is called an integral basis of $`𝒪`$ if $`𝒪=\{m\alpha +n\beta :m,n\}`$. Hence $`\{1,\omega \}`$ is an integral basis but the choice is not unique (cf. proposition 12 later).

###### Proposition 6

If $`\{\alpha ,\beta \}`$ is any integral basis of $`𝒪`$ then

$$D=\left|\begin{array}{cc}\alpha & \beta \\ \overline{\alpha }& \overline{\beta }\end{array}\right|^2$$

is a positive integer independent of the choice of basis. $`D`$ is called the discriminant of $`[\sqrt{d}]`$. In fact if $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`D=d`$ and if $`d2,3\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`D=4d`$.

Proof Let $`\{\alpha ,\beta \}`$ be an integral basis. Since $`\{1,\omega \}`$ is an integral basis we have

$$\left(\begin{array}{cc}\alpha & \beta \end{array}\right)=\left(\begin{array}{cc}1& \omega \end{array}\right)\left(\begin{array}{cc}a_1& a_2\\ b_1& b_2\end{array}\right)\left(\begin{array}{cc}1& \omega \end{array}\right)M$$

for some integers $`a_1,a_2,b_1,b_2`$. Hence

$$\left(\begin{array}{cc}1& \omega \end{array}\right)=\left(\begin{array}{cc}\alpha & \beta \end{array}\right)M^1$$

but $`\{\alpha ,\beta \}`$ is an integral basis too so $`M^1`$ must have integer entries. Since $`M`$ and $`M^1`$ both have integer entries it follows that $`detM=\pm 1`$ (because $`detM`$ and $`detM^1=1/detM`$ are both integers). Thus

$$\left|\begin{array}{cc}\alpha & \beta \\ \overline{\alpha }& \overline{\beta }\end{array}\right|^2=\left|\left(\begin{array}{cc}1& \omega \\ 1& \overline{\omega }\end{array}\right)M\right|^2=|M|^2\left|\begin{array}{cc}1& \omega \\ 1& \overline{\omega }\end{array}\right|^2=\left|\begin{array}{cc}1& \omega \\ 1& \overline{\omega }\end{array}\right|^2.$$

Using the explicit values of $`\omega `$ in theorem 2 we get:
if $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`D=d`$;
if $`d2,3\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`D=4d`$.

Hence an integer $`D`$ is the discriminant of some $`[\sqrt{d}]`$ iff $`D1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ or else $`D0\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ and then $`\frac{D}{4}2,3\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$. Using the notion of discriminant we can give a unified description of $`𝒪`$ (without having to separate two cases of $`d\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$). Indeed it will often be easier to work with $`D`$ rather than $`d`$.

###### Proposition 7

Let $`D`$ be the discriminant of $`[\sqrt{d}]`$. Then in all cases $`𝒪=[\frac{D+\sqrt{D}}{2}]`$.

Proof This follows immediately from the above values of $`D`$ and $`\omega `$ for the various cases of $`d\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$.

## 3 Algebraic integers and Pell’s equation

We now establish the connection between algebraic integers and Pell’s equation.

An algebraic integer $`\xi 𝒪`$ is called a unit if it has a multiplicative inverse that is also an algebraic integer. According to this definition, in the usual integers $``$ (or rationals $``$), there are only two units viz. $`\pm 1`$.

###### Proposition 8

$`\xi =x+y\sqrt{d}𝒪`$ is a unit iff $`2x`$ and $`x^2dy^2=\pm 1`$ (i.e. we have a strengthening of the conditions in proposition 5) .

Proof Recall that for any $`\xi [\sqrt{d}]`$

$$\frac{1}{\xi }=\frac{\overline{\xi }}{\xi \overline{\xi }}=\frac{xy\sqrt{d}}{x^2dy^2}.$$

By proposition 5 $`\xi 𝒪`$ iff $`2x`$ and $`x^2dy^2`$ and for $`\frac{1}{\xi }`$ to be in $`𝒪`$ we also require $`\frac{2x}{x^2dy^2}`$ and $`\frac{x^2dy^2}{(x^2dy^2)^2}=\frac{1}{x^2dy^2}`$ i.e. $`x^2dy^2=\pm 1`$.

Hence (in contrast to $``$) there are infinitely many units in $`𝒪`$ and they are intimately related to solutions of Pell’s equation i.e. solutions of Pell’s equation can be viewed as a natural generalisation of the fundamental integers $`\pm 1`$.

###### Proposition 9

If $`a+b\sqrt{d}>1`$ is a unit then $`a>0`$ and $`b>0`$.

Proof Arguing by contradiction, suppose that $`\xi =xy\sqrt{d}>1`$ is a unit with $`x>0,y>0`$. Then

$$0<\frac{1}{\xi }=\frac{x+y\sqrt{d}}{x^2dy^2}<1.$$

But $`x^2dy^2=\pm 1`$ so $`\frac{1}{\xi }>0`$ gives $`x^2dy^2=1`$ and then $`x+y\sqrt{d}<1`$. Now $`x^2dy^2=1`$ implies $`x1`$ in contradiction to $`x+y\sqrt{d}<1`$. Similarly $`x+y\sqrt{d}`$ cannot be a unit if it is $`>1`$.

A slight generalisation of theorem 1 characterises all units:

###### Theorem 3

Let $`ϵ_0`$ be the smallest unit in $`𝒪`$ that is greater than 1. Then the set of all units is given by $`\{\pm ϵ_0^k`$: $`k\}`$.

$`ϵ_0`$ is called the fundamental unit.

Proof The proof is very similar to that of theorem 1 and we omit duplicating the details.

Remark If $`d2,3\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`𝒪=[\sqrt{d}]`$ so if the fundamental unit $`ϵ_0=m+n\sqrt{d}`$ has $`m^2dn^2=1`$ then the units are exactly the solutions of Pell’s equation. If $`m^2dn^2=1`$ then $`ϵ_1=ϵ_0^2=m^{}+n^{}\sqrt{d}`$ with $`m^{}=2m^2+1`$ and $`n^{}=2mn`$ has $`m^2dn^2=ϵ_0^2\overline{ϵ}_0^2=(1)^2=1`$ and it generates all solutions of Pell’s equation via theorem 1.
If $`d1\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}4}`$ then $`𝒪=[\frac{1+\sqrt{d}}{2}]`$ so some units may have rational coefficients $`r_1+r_2\sqrt{d}`$ with denominator 2 (e.g. $`d=5`$ has $`\frac{1\pm \sqrt{5}}{2}`$ as units). But given the fundamental unit we can generate all units in numerical order as powers and select the smallest unit $`ϵ=x+y\sqrt{d}>1`$ having $`x^2dy^2=1`$ and $`x,y`$. Then via theorem 1 we can again generate all solutions of Pell’s equation.

Hence solving Pell’s equation is equivalent to finding the fundamental unit of the algebraic integers in $`[\sqrt{d}]`$. We define the regulator $`R`$ of $`𝒪`$ by

$$R=\mathrm{ln}ϵ_0$$

and our task is now to compute an $`n`$ digit approximation to $`R`$ in poly$`(\mathrm{log}d,n)`$ time. To define our basic function $`h`$ with period $`R`$ we will need the concept of an ideal of $`𝒪`$ in $`[\sqrt{d}]`$ and more specifically, the notion of reduced principal ideals.

## 4 Ideals of the algebraic integers

If $`A`$ and $`B`$ are subsets of $`𝒪`$ or $`[\sqrt{d}]`$ we define the product $`AB`$ to be the additive span of all products $`ab`$ with $`aA`$ and $`bB`$

$$AB=\{a_1b_1+\mathrm{}+a_nb_n:a_1,\mathrm{},a_nA,b_1,\mathrm{},b_nB,n\}.$$

###### Definition 1

$`I𝒪`$ is an (integral) ideal of $`𝒪`$ if $`I𝒪=I`$ and $`\alpha ,\beta I`$ implies $`m\alpha +n\beta I`$ for all $`m,n`$.
$`I[\sqrt{d}]`$ is a fractional ideal of $`𝒪`$ if $`I𝒪=I`$ and $`\alpha ,\beta I`$ implies $`m\alpha +n\beta I`$ for all $`m,n`$.

In other words an integral (respectively fractional) ideal of $`𝒪`$ is a subset of $`𝒪`$ (respectively $`[\sqrt{d}]`$) that is closed under forming $``$-linear combinations of its elements and also closed under multiplication by elements of $`𝒪`$. In standard algebra the basic object of study ($`𝒪`$ here) is generally not embedded in an ambient structure ($`[\sqrt{d}]`$ here) and the term ‘ideal’ corresponds to ‘integral ideal’ above.

Example In $``$ we have $`𝒪=`$ (the usual integers) and $`I_m=m=\{0,\pm m,\pm 2m,\mathrm{}\}`$ is an integral ideal for each $`m`$ as can easily be verified. Similarly for $`𝒪[\sqrt{d}]`$ if $`\alpha 𝒪`$ is any chosen element then $`I_\alpha =\alpha 𝒪=\{\alpha \xi :\xi 𝒪\}`$ is always an integral ideal (or fractional ideal if more generally $`\alpha [\sqrt{d}]`$). It can be shown that for $``$ the $`I_m`$’s are the only integral ideals whereas for $`𝒪[\sqrt{d}]`$, generally the $`I_\alpha `$’s do not exhaust all possible ideals.

### 4.1 Principal ideals and periodicity

###### Definition 2

If $`\gamma 𝒪`$ (respectively $`[\sqrt{d}]`$) then the set $`\gamma 𝒪=\{\gamma \xi :\xi 𝒪\}`$ is always an integral (respectively fractional) ideal and ideals of this form are called principal ideals.

###### Proposition 10

$`\alpha 𝒪=\beta 𝒪`$ iff $`\alpha =\beta ϵ`$ where $`ϵ`$ is a unit in $`[\sqrt{d}]`$.

Proof If $`ϵ`$ is a unit then it is easy to see that $`ϵ𝒪=𝒪`$. Hence if $`\alpha =\beta ϵ`$ then $`\alpha 𝒪=\beta ϵ𝒪=\beta 𝒪`$. Conversely suppose that $`\alpha 𝒪=\beta 𝒪`$. Since $`1𝒪`$ we have $`\alpha \alpha 𝒪=\beta 𝒪`$ so there is $`\eta _1𝒪`$ with $`\alpha =\beta \eta _1`$. Interchanging roles of $`\alpha `$ and $`\beta `$ we get $`\eta _2𝒪`$ with $`\beta =\eta _2\alpha `$ so $`\alpha =\beta \eta _1=\alpha \eta _2\eta _1`$ i.e. $`\eta _2\eta _1=1`$ and $`\eta _1,\eta _2`$ are units.

Proposition 10 with theorem 3 provides the key to converting our basic task (of computing regulators) into a periodicity problem. By theorem 3 we know that $`ϵ`$ is a unit iff $`ϵ=ϵ_0^k`$ where $`ϵ_0`$ is the fundamental unit. Let $`𝒫=\{\xi 𝒪:\xi [\sqrt{d}]\}`$ be the set of all fractional principal ideals. Thus if we consider the ideal $`\xi 𝒪𝒫`$ as a function of $`x=\mathrm{ln}\xi `$ i.e.

$$g(x)=e^x𝒪,$$

then $`g`$ will be a periodic function with our desired period $`R=\mathrm{ln}ϵ_0`$. However the direct use of this function would appear to be computationally problematic for various reasons. In order to compute $`g`$ and see the periodicity we would need to be able to determine that $`e^x𝒪`$ and $`e^x^{}𝒪`$ are the same ideal when $`x^{}=x+R`$ and furthermore we would need to compute $`g`$ (in quantum superposition) for values of $`xO(R)`$ or larger. In that case, even the integer part of $`e^x`$ (being $`O(ϵ_0)O(e^\sqrt{d})`$) would have exponentially many digits. Thus the arithmetic operations needed to see that $`e^x𝒪=e^{x+R}𝒪`$ would presumably require exponential time $`O(\mathrm{poly}(\mathrm{log}d,\mathrm{log}e^x))=O(\mathrm{poly}(d))`$. Furthermore the sets $`[\sqrt{d}]`$ and $`𝒪`$ are dense in $``$ and we have infinitely many distinct (integral) ideals. Thus the identification of the ideal $`\alpha 𝒪`$ would generally depend on $`\alpha `$ to full (infinite) precision (or alternatively we would need to formulate some effectively computable notion of two ideals $`I`$ and $`I^{}`$ being “almost the same”.)

To get around these difficulties we will utilise the notion of reduced principal ideals $`I`$ (a concept which already appears in Gauss’ Disquisitiones Arithmeticae of 1801) and a notion of distance $`\delta (I)`$ of $`I`$ from the unit ideal $`𝒪`$ (which was introduced by D. Shanks in 1972 ). The set $`𝒫_{\mathrm{red}}`$ of reduced principal ideals will be a finite set (although exponentially large in $`\mathrm{log}d`$). Each reduced ideal will have a $`\mathrm{poly}\mathrm{log}d`$ sized description so we avoid the above problems of infinite precision. Furthermore we will have an (efficiently computable) operation $`\rho :𝒫_{\mathrm{red}}𝒫_{\mathrm{red}}`$ which allows us to cycle through the set of reduced ideals in order of increasing distance from $`𝒪`$ and we will have an effective means of jumping by exponentially large distances. With each successive application of these processes (starting on $`𝒪`$) we will be able to efficiently compute the distance increment and the accumulated distance passes through $`R`$ as the ideal cycle returns to $`𝒪`$. Once these, and some further ingredients are in place we will be able to define an efficiently computable function on $``$ with period $`R`$ and apply the quantum period-finding algorithm (suitably generalised for irrational periods on $``$).

## 5 Presentations of ideals

###### Proposition 11

Any principal fractional ideal $`I`$ has the form

$$I=\alpha +\beta =\{m_1\alpha +m_2\beta :m_1,m_2\}$$

where $`\alpha ,\beta [\sqrt{d}]`$ are linearly independent over $``$.

Proof Since $`𝒪=+\omega `$, for $`\gamma 𝒪`$ we can take $`\alpha =\gamma `$ and $`\beta =\gamma \omega `$. Furthermore $`1,\omega `$ are linearly independent over $``$ so $`\alpha ,\beta `$ must be too.

Remark Proposition 11 is actually true for any (not necessarily principal) fractional ideal of $`𝒪`$ but we will not need this more general fact.

Thus intuitively any ideal is like a “2-dimensional vector space over $``$” with the extra property of being closed under multiplication by $`𝒪`$ (which restricts the possible choices of $`\alpha `$ and $`\beta `$). Any set $`\{\alpha ,\beta \}`$ in proposition 11 is called an integral basis of the fractional ideal. Changes of basis must respect the restriction that coefficients are required to be integers.

###### Proposition 12

Let $`\{\alpha ,\beta \}`$ be an integral basis of a fractional ideal $`I`$. Then $`\{\alpha ^{},\beta ^{}\}`$ is another integral basis iff

$$\left(\begin{array}{c}\alpha ^{}\\ \beta ^{}\end{array}\right)=M\left(\begin{array}{c}\alpha \\ \beta \end{array}\right)$$
(6)
where $`M`$ is a $`2\times 2`$ matrix with integer entries and $`detM=\pm 1`$.

Proof ($``$) Since $`\alpha ^{},\beta ^{}I`$, eq. (6) must hold for some matrix $`M`$ with integer entries. By moving $`M`$ to LHS as $`M^1`$ we see that $`M^1`$ must also have integer entries so (just as in proposition 6) we must have $`detM=\pm 1`$.
($``$) Conversely if eq. (6) holds with $`detM=\pm 1`$ then each of $`\alpha +\beta `$ and $`\alpha ^{}+\beta ^{}`$ is contained in the other since the two sets $`\{\alpha ,\beta \}`$ and $`\{\alpha ^{},\beta ^{}\}`$ are related as linear combinations with integer coefficients.

If $`\{\alpha ,\beta \}`$ is any integral basis of a fractional ideal $`I`$ then we introduce the absolute value

$$𝒩(I)=\left|det\left(\begin{array}{cc}\alpha & \beta \\ \overline{\alpha }& \overline{\beta }\end{array}\right)\right|/\sqrt{D}$$

###### Proposition 13

$`𝒩(I)`$ is independent of the choice of integral basis. If $`I=\gamma 𝒪`$ is a principal integral ideal then $`𝒩(I)`$ is the integer $`|\gamma \overline{\gamma }|`$.

Proof If $`\{\alpha ^{},\beta ^{}\}`$ is any other integral ideal then $`(\alpha ^{}\beta ^{})=(\alpha \beta )M`$ where $`detM=\pm 1`$. Hence

$$det\left(\begin{array}{cc}\alpha ^{}& \beta ^{}\\ \overline{\alpha ^{}}& \overline{\beta ^{}}\end{array}\right)=detMdet\left(\begin{array}{cc}\alpha & \beta \\ \overline{\alpha }& \overline{\beta }\end{array}\right)=\pm det\left(\begin{array}{cc}\alpha & \beta \\ \overline{\alpha }& \overline{\beta }\end{array}\right).$$

If $`I=\gamma 𝒪`$ we can take $`\alpha =\gamma `$ and $`\beta =\gamma (D+\sqrt{D})/2`$ and compute $`𝒩(I)`$ directly giving $`|\gamma \overline{\gamma }|`$ which is an integer for $`\gamma 𝒪`$ by proposition 5 .

###### Proposition 14

Any fractional ideal $`I`$ has an integral basis $`\{\alpha ,\beta \}`$ with $`\alpha >0`$ rational. Furthermore $`\alpha `$ is uniquely determined as the least positive rational in $`I`$. If $`I`$ is an integral ideal then $`\alpha `$ is an integer (and so $`I`$ contains no rationals that are not integers).

Proof If $`\alpha =r_1+r_2\sqrt{d},\beta =s_1+s_2\sqrt{d}`$ with $`r_1,r_2,s_1,s_2`$ is any integral basis there exist integers $`m,n`$ such that $`mr_2+ns_2=0`$ and $`\mathrm{gcd}(m,n)=1`$. Thus by Euclid’s algorithm there exist integers $`a,b`$ such that $`ambn=\mathrm{gcd}(m,n)=1`$ i.e. the matrix

$$M=\left(\begin{array}{cc}m& n\\ a& b\end{array}\right)$$

has $`detM=1`$. Then the integral basis

$$\left(\begin{array}{cc}\alpha ^{}& \\ \beta ^{}& \end{array}\right)=M\left(\begin{array}{cc}\alpha & \\ \beta & \end{array}\right)$$

has $`\alpha ^{}`$ rational (which we may take to be $`>0`$ by a change of sign). Since $`\{\alpha ^{},\beta ^{}\}`$ is linearly independent over $``$, $`\beta ^{}`$ cannot be rational. Any element $`\xi `$ of $`I`$ can be written $`\xi =m\alpha ^{}+n\beta ^{}`$ so $`\xi `$ is rational iff $`n=0`$. Thus $`\alpha ^{}`$ is the least positive rational in $`I`$.
If $`I`$ is an integral ideal then $`I𝒪=[\frac{D+\sqrt{D}}{2}]=\{m+n\frac{D+\sqrt{D}}{2}:m,n\}`$ where $`\sqrt{D}`$ is irrational. Hence the only rationals in $`I`$ are integers so $`\alpha ^{}`$ must be an integer.

###### Proposition 15

$`I[\sqrt{d}]`$ is a fractional ideal iff there is $`m`$ such that $`mI`$ is an integral ideal (hence justifying the terminology ‘fractional ideal’).

Proof $`()`$ If $`mI`$ is an integral ideal the clearly $`I=\frac{1}{m}(mI)`$ is a fractional ideal.
$`()`$ Let $`\{\alpha ,\beta \}`$ be an integral basis of $`I`$ and let $`k`$ be any integer such that $`k\alpha ,k\beta [\sqrt{d}]`$ i.e. all denominators of rational coefficients in $`\alpha ,\beta `$ divide $`k`$. Then $`kI`$ is an ideal and $`kI[\sqrt{d}]𝒪`$ i.e. $`kI`$ is an integral ideal.

Recall that $`a+\frac{b}{2}=a+\frac{b^{}}{2}`$ for any $`b^{}b\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}a`$ so we can adjust the value of $`b`$ to lie in any desired interval of length $`2a`$. We will make use of the following basic choice:
For $`a,b`$ with $`a0`$ let $`\tau (b,a)`$ be the unique integer $`\tau `$ such that $`\tau b\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}a`$ and
$`a<\tau a`$ if $`a>\sqrt{D}`$,
$`\sqrt{D}2a<\tau \sqrt{D}`$ if $`a<\sqrt{D}`$.

###### Proposition 16

A subset $`I[\sqrt{d}]`$ is an integral ideal of $`𝒪`$ iff $`I`$ can be written as

$$I=k\left(a+\frac{b+\sqrt{D}}{2}\right)$$
(7)
where $`a,b,k`$ with $`a>0`$, $`k>0`$, $`b=\tau (b,a)`$ and $`4a`$ divides $`b^2D`$.
Furthermore this presentation of the ideal $`I`$ as the triple of integers $`(a,b,k)`$ is unique: $`ak`$ is the least positive rational in $`I`$, $`k/2`$ is the least positive coefficient of $`\sqrt{D}`$ of any member of $`I`$ and $`b=\tau (b,a)`$ uniquely determines $`b`$. Also $`𝒩(I)=k^2a`$.

Proof ($``$) Suppose that $`I`$ is an integral ideal of $`𝒪`$. By proposition 14 we have

$$I=a^{}+\beta \text{where }a^{}\text{ is the least positive integer in }I\text{.}$$

$`\beta I𝒪`$ so we can write $`\beta =m_1+\frac{D+\sqrt{D}}{2}m_2=\frac{b^{}+k\sqrt{D}}{2}`$ for integers $`b^{},k`$. Since $`\beta =\beta `$ we can assume that $`k>0`$. Now $`a^{}I`$ and $`\frac{D+\sqrt{D}}{2}𝒪`$ so their product is in $`I`$ so there exist integers $`m_1,m_2`$ with

$$a^{}\left(\frac{D+\sqrt{D}}{2}\right)=m_1a^{}+m_2\beta =m_1a^{}+m_2(\frac{b^{}+k\sqrt{D}}{2}).$$

Equating coefficients of $`\sqrt{D}`$ gives $`a^{}=m_2k`$ so $`k`$ divides $`a^{}`$. Also $`\frac{b^{}+k\sqrt{D}}{2}I`$ so similarly, there exist integers $`n_1,n_2`$ with

$$(\frac{b^{}+k\sqrt{D}}{2})(\frac{D+\sqrt{D}}{2})=n_1a^{}+n_2(\frac{b^{}+k\sqrt{D}}{2}).$$

Equating coefficients of $`\sqrt{D}`$ then shows that $`k`$ divides $`b^{}`$. Writing $`a^{}=ka`$ and $`b^{}=kb`$ we get $`I=k(a+\frac{b+\sqrt{D}}{2})`$. In this expression $`k/2`$ is uniquely determined as the least positive coefficient of $`\sqrt{D}`$ in any member of $`I`$. Also $`ka`$ is uniquely determined as the least positive rational in $`I`$ so $`a`$ is unique too. The condition $`b=\tau (b,a)`$ then uniquely fixes the value of $`b`$. Finally we show that $`4a`$ divides $`Db^2`$. Since $`k(b+\sqrt{D})/2I`$ and $`(D+\sqrt{D})/2𝒪`$ we have

$$\left(\frac{b+\sqrt{D}}{2}\right)\left(\frac{D+\sqrt{D}}{2}\right)=k\frac{(b+1)D+(b+D)\sqrt{D}}{4}I.$$

Hence it must be of the form $`k(xa+y\frac{b+\sqrt{D}}{2})`$ for $`x,y`$. Thus $`(b+D)=2y`$ and $`D(b+1)=4ax+2yb=4ax+b^2+bD`$ giving $`4ax=Db^2`$ i.e. $`4a`$ divides $`Db^2`$.
($``$) Conversely suppose that $`I`$ has the form given in eq. (7) with $`a,b,k`$ satisfying the given conditions. We show that $`I`$ is then an ideal of $`𝒪`$. Now $`I`$ is clearly closed under $``$-linear combinations of its members so it remains to show that $`𝒪I=I`$. Since $`𝒪`$ has integral basis $`\{1,\omega =\frac{D+\sqrt{D}}{2}\}`$ it then suffices to show that $`1II`$ and $`\omega II`$. The first is obvious and for the second it suffices to show $`ka\omega I`$ and $`k(\frac{b+\sqrt{D}}{2})\omega I`$. Now $`ka\omega I`$ iff

$$ka\omega =k\left(\frac{aD}{2}+\frac{a}{2}\sqrt{D}\right)=m_1ka+m_2k\left(\frac{b+\sqrt{D}}{2}\right)$$

has a solution $`m_1,m_2`$ in integers. Equating coefficients gives $`m_1a+m_2b/2=aD/2`$ and $`m_2/2=a/2`$ so $`m_2=a`$ and $`m_1=(D+b)/2`$. But $`b^2=D+4ac`$ so $`b^2D\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$ so $`bD\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$ (as $`b`$ and $`b^2`$ always have the same parity). Hence $`m_1,m_2`$ are integers as required.
A similar calculation for $`k(\frac{b+\sqrt{D}}{2})\omega `$ gives $`m_2=(D+b)/2`$ which is an integer and $`m_1=\frac{Db^2}{4a}`$ which is an integer by hypothesis.

Finally $`𝒩(I)=k^2a`$ follows by direct calculation with the integral basis $`\{ka,k\frac{b+\sqrt{D}}{2}\}`$.

Remark A principal ideal $`I=\alpha 𝒪`$ can be given either by giving a value of $`\alpha `$ or by giving the parameters $`a,b,k`$. Although there is a $`O(\mathrm{poly}(\mathrm{log}\alpha ,\mathrm{log}D))`$ time algorithm for translating $`\alpha `$ into $`(a,b,k)`$ (cf. proposition 17 below) the reverse translation appears to be a hard computational task (classically) – the best known classical algorithm has running time $`O(e^{\sqrt{\mathrm{log}d}})`$. Thus this interconversion corresponds to a one-way function which forms the basis of the Buchmann-Williams cryptosystem  for key exchange (which can be broken by an extension  of Hallgren’s algorithm).

###### Proposition 17

Let $`\alpha =\frac{x+y\sqrt{D}}{2}𝒪`$ with $`x,y`$. Let $`k=\mathrm{gcd}(y,(x+yD)/2)`$ and $`u,v`$ such that

$$uy+v(x+yD)/2=k$$

(which are guaranteed to exist by Euclid’s algorithm). Then

$$\alpha 𝒪=k(a+\frac{b+\sqrt{D}}{2})$$

with $`k`$ as above, $`a=|\alpha \overline{\alpha }|/k^2`$ and $`b=\tau ((ux+y(x+yD)/2)/k,a)`$. Hence the parameters $`(a,b,k)`$ of $`\alpha 𝒪`$ can be computed in $`\mathrm{poly}(\mathrm{log}|x|,\mathrm{log}|y|,\mathrm{log}D)`$ time.

Proof Since $`𝒪=+\frac{D+\sqrt{D}}{2}`$, it follows that any $`\alpha 𝒪`$ may be written as $`\alpha =x^{}+y^{}\frac{D+\sqrt{D}}{2}`$ for $`x^{},y^{}`$ so $`\alpha =\frac{x+y\sqrt{D}}{2}`$ with $`x,y`$ and $`(x+yD)/2`$. Also then, $`\alpha 𝒪`$ is generated (via $``$-linear combinations) by $`(x+y\sqrt{D})/2`$ and $`(x+y\sqrt{D})(D+\sqrt{D})/4=((x+y)D+(x+yD)\sqrt{D})/4`$. Since $`k`$ is the smallest positive coefficient of $`\sqrt{D}/2`$ we get $`k=\mathrm{gcd}(y,(x+yD)/2)`$. Also $`𝒩(\alpha 𝒪)=k^2a=|\alpha \overline{\alpha }|`$ giving the claimed formula for $`a`$.

The only elements of $`\alpha 𝒪`$ with $`\sqrt{D}`$ coefficient $`k/2`$ have the form $`k(aw+\frac{b+\sqrt{D}}{2}1)=k(b^{}+\sqrt{D})/2`$ where $`b^{}=b+2wa`$ and $`w`$, so then $`b=\tau (b^{},a)`$. Now if $`k=uy+v(x+yD)/2`$ for $`u,v`$ then $`u\frac{x+y\sqrt{D}}{2}+v((x+y)D+(x+yD)\sqrt{D})/4`$ is in $`\alpha 𝒪`$ and has the form $`k(b^{}+\sqrt{D})/2`$ where $`b^{}=(ux+v(x+y)/2)/k`$. Then $`b=\tau (b^{},a)`$ gives the claimed formula for $`b`$.

Finally in $`k=uy+v(x+yD)/2`$, $`u`$ and $`v`$ might conceivably be very large but the choice is not unique – we have the freedoms $`uus(x+yD)/2`$ and $`vv+sy`$ for any $`s`$. Hence we can take $`v<y`$ and then $`u=(k=v(x+y)D/2)/y`$ is also suitably small making the whole computation of $`a,b,k`$ performable in $`\mathrm{poly}(\mathrm{log}|x|,\mathrm{log}|y|,\mathrm{log}D)`$ time.

Using proposition 15 with proposition 16 we get a corresponding unique presentation of a fractional ideal as

$$I=\frac{k}{l}\left(a+\frac{b+\sqrt{D}}{2}\right)$$
(8)
with $`l`$ being the smallest such integer and $`a,b,k`$ satisfying the same conditions as above. When the parameters $`a,b,k,l`$ satisfy these conditions, making them unique, we say that $`I`$ is in standard form.

## 6 Reduced ideals and the reduction operator $`\rho `$

### 6.1 Reduced ideals

It will be helpful to introduce a geometrical picture of a fractional ideal $`I`$ as a two dimensional lattice embedded in $`^2`$: if $`\alpha I`$ we map it to the point $`\widehat{\alpha }=(\alpha ,\overline{\alpha })^2`$ (recalling that for $`\alpha =p+q\sqrt{d}`$ we define $`\overline{\alpha }=pq\sqrt{d}`$).

###### Definition 3

A minimum of $`I`$ is an element $`\alpha I`$ such that $`\alpha >0`$ and there is no nonzero $`\beta I`$ with $`|\beta |<|\alpha |`$ and $`|\overline{\beta }|<|\overline{\alpha }|`$ i.e. $`\widehat{\alpha }^2`$ lies in the right half plane and the rectangle defined by $`\widehat{\alpha }`$ (having corners at the four points $`(\pm \alpha ,\pm \overline{\alpha })`$ contains no lattice points inside it (except for $`(0,0)`$).
A fractional ideal $`I`$ is called reduced if $`1I`$ and $`1`$ is a minimum of $`I`$.

###### Proposition 18

If $`I`$ is reduced then its standard form is

$$I=+\frac{b+\sqrt{D}}{2a}$$

i.e. in eq. (8) we have $`k=1`$ and $`l=a`$.

Proof Since $`1I`$ we have $`1=\frac{k}{l}(xa+yb+y\sqrt{D})`$ with $`x,y`$. Hence $`y=0`$ and $`kxa/l=1`$. But $`1I`$ is also a minimum so $`x=1`$ (because if $`x>1`$ then $`\beta =k(x1)a/l<1`$ is an element of $`I`$ with $`0<\beta <1`$ and $`0<\overline{\beta }<1`$). Thus $`ka=l`$ so $`k`$ divides $`l`$ and the minimality of $`l`$ in eq. (8) implies $`k=1`$.

###### Proposition 19

Let

$$I=+\frac{b+\sqrt{D}}{2a}$$

be a reduced ideal in standard form. Then $`a<\sqrt{D}`$ and $`|b|<\sqrt{D}`$. Hence the number of reduced ideals is finite.

Proof From the definition of $`\tau `$ and $`b=\tau (b,a)`$ we have $`|b|<\sqrt{D}`$ if $`a<\sqrt{D}`$. To show $`a<\sqrt{D}`$ suppose on the contrary that $`a>\sqrt{D}`$. Then from the definition of $`\tau `$ we have $`|b|<a`$ so $`|\frac{b+\sqrt{D}}{2a}|<1`$ and $`|\frac{b\sqrt{D}}{2a}|<1`$ contradicting the fact that 1 is a minimum (since $`\frac{b+\sqrt{D}}{2a}I`$). Hence $`a<\sqrt{D}`$.

###### Proposition 20

Suppose a fractional ideal has the standard form

$$I=+\frac{b+\sqrt{D}}{2a}.$$

Then $`I`$ is reduced iff $`b0`$ and $`b+\sqrt{D}>2a`$.

Proof ($``$) Assume $`I`$ is reduced. By proposition 19 $`a<\sqrt{D}`$ and so $`b=\tau (b,a)`$ lies in the range $`\sqrt{D}2a<b<\sqrt{D}`$. If $`b<0`$ then $`|b|=b<2a\sqrt{D}`$ i.e. $`|b|+\sqrt{D}<2a`$ which contradicts the minimality of 1 (as $`\mathrm{max}(|\frac{b+\sqrt{D}}{2a}|,|\frac{b\sqrt{D}}{2a}|)=\frac{|b|+\sqrt{D}}{2a}`$). Hence $`b0`$ and if $`\frac{b+\sqrt{D}}{2a}<1`$ then $`\mathrm{max}(|\frac{b+\sqrt{D}}{2a}|,|\frac{b\sqrt{D}}{2a}|)=\frac{|b|+\sqrt{D}}{2a}=\frac{b+\sqrt{D}}{2a}<1`$ again contradicting the minimality of 1. Hence $`b+\sqrt{D}>2a`$.
($``$) Conversely assume that $`b0`$ and $`b+\sqrt{D}>2a`$. Let $`H(x,y)=\mathrm{max}(|2xa+y(b+\sqrt{D})|,|2xa+y(b\sqrt{D})|)`$. Then $`I`$ is reduced iff 1 is a minimum of $`I`$ iff $`H(x,y)>2a`$ for all $`x,y`$ with $`(x,y)(0,0)`$. Since $`H(x,y)=H(x,y)`$ it suffices to consider the two sectors $`x0,y0`$ and $`x>0,y0`$.
If $`x0,y0`$ and $`(x,y)(0,0)`$ then $`b+\sqrt{D}>2a`$ gives $`|2xa+y(b+\sqrt{D})|2a(x+y)2a`$ so $`H(x,y)2a`$ in this sector.
For $`x>0,y0`$ we first show that $`b\sqrt{D}<0`$. Indeed $`b+\sqrt{D}>2a`$ gives $`a<\sqrt{D}+(ba)`$. If $`a>\sqrt{D}`$ then the definition of $`\tau `$ gives $`b<a`$ and the contradiction $`a<\sqrt{D}`$. Hence $`a<\sqrt{D}`$ and the definition of $`\tau `$ again gives $`b<\sqrt{D}`$ i.e. $`b\sqrt{D}<0`$. Then $`|2xa+y(b\sqrt{D})|=2ax+|y||b\sqrt{D}|2xa`$. Since $`x>0`$ we get $`H(x,y)2a`$.

###### Corollary 1

The fractional ideal $`I=+\frac{b+\sqrt{D}}{2a}`$ is reduced if $`a\sqrt{D}/2`$.

Proof If $`a\sqrt{D}/2`$ then the definition of $`\tau `$ gives $`b>0`$ and then also $`b+\sqrt{D}b+2a>2a`$ so by proposition 20 $`I`$ is reduced.

### 6.2 The reduction operator $`\rho `$

For a fractional (not necessarily reduced) ideal of the form

$$I=+\frac{b+\sqrt{D}}{2a}$$

we introduce the notation

$$\gamma (I)=\frac{b+\sqrt{D}}{2a}$$
(9)
and define the reduction operator $`\rho `$ mapping (principal) ideals to (principal) ideals by

$$\rho (I)=\frac{1}{\gamma (I)}I=\frac{2a}{b+\sqrt{D}}+.$$

Since $`4a`$ divides $`b^2D`$ (cf. proposition 16) we introduce the integer $`c=|Db^2|/(4a)`$ and then

$$\frac{2a}{b+\sqrt{D}}=\frac{2a(b\sqrt{D})}{b^2D}=\pm \frac{b\sqrt{D}}{2c}.$$

Thus (as $`=\pm `$)

$$\frac{2a}{b+\sqrt{D}}+=\frac{b+\sqrt{D}}{2c}+=+\frac{\tau (b,c)+\sqrt{D}}{2c}.$$

Hence the standard parameters $`a^{},b^{}`$ of $`\rho (I)=+\frac{b^{}+\sqrt{D}}{2a^{}}`$ are

$$b^{}=\tau (b,c)a^{}=c=\frac{|Db^2|}{4a}.$$
(10)
Remark Many treatments of this subject use a slightly more general notion of reduction. Recall that a general fractional ideal has the standard form

$$I=\frac{k}{l}\left(a+\frac{b+\sqrt{D}}{2}\right)=\frac{ak}{l}\left(+\frac{b+\sqrt{D}}{2a}\right)$$

with $`a,b,k,l`$ uniquely determined. Write $`\gamma (I)=\frac{b+\sqrt{D}}{2a}`$. Then in the more general formalism the reduction operator is defined by $`\rho (I)=\frac{1}{\gamma (I)}I`$ and $`I`$ is said to be reduced if $`ka/l`$ (the smallest positive rational in $`I`$) is a minimum. Thus $`I`$ is reduced iff $`+\gamma (I)`$ is reduced in our restricted sense (i.e. for the restricted sense that we’re using, we also require 1 to be the smallest positive rational in $`I`$).

###### Proposition 21

Let $`I=+\frac{b+\sqrt{D}}{2a}`$ be a (not necessarily reduced) fractional ideal. Let $`I_0=I`$ and $`I_i=\rho (I_{i1})=+\frac{b_i+\sqrt{D}}{2a_i}`$. If $`I_i`$ is not reduced then $`a_i<a_{i1}/2`$ and thus $`I_i`$ is reduced for some $`i\mathrm{log}_2(a/\sqrt{D})+1`$. Let $`i_{\mathrm{red}}`$ be the minimal such $`i`$. Then $`\alpha =_{j=1}^{i_{\mathrm{red}}1}\gamma (I_j)`$ is a minimum in $`I`$ and $`I_{\mathrm{red}}=I_{i_{\mathrm{red}}}=\frac{1}{\alpha }I`$.

Proof If $`a_{i1}>\sqrt{D}`$ then $`|b_{i1}|<a_{i1}`$ so

$$a_i=\frac{|b_{i1}^2D|}{4a_{i1}}<\frac{a_{i1}^2+D}{4a_{i1}}<\frac{a_{i1}}{2}.$$

Hence $`a_i<\sqrt{D}`$ for some $`i\mathrm{log}(a/\sqrt{D})`$.
Now assume that $`a_i<\sqrt{D}`$ and consider the two cases $`a_{i+1}a_i`$ and $`a_{i+1}<a_i`$. In the first case, we have $`|b_i^2D|/(4a_i)a_i`$ and also $`\sqrt{D}2a_i<b_i<\sqrt{D}`$ so $`Db_i^24a_i^2`$ giving $`\sqrt{D}+|b_i|>2a_i`$. Next we show that $`b_i0`$, so by proposition 20, $`I`$ will be reduced. If $`b_i<0`$ then $`\sqrt{D}2a_i<b_i<0`$ gives $`|b_i|<|\sqrt{D}2a_i|=2a_i\sqrt{D}`$ in contradiction to $`\sqrt{D}+|b_i|>2a_i`$. Hence $`b_i0`$.
In the second case: if $`a_{i+1}<a_i`$ then $`a_{i+1}^2<a_ia_{i+1}=a_i|\frac{b_i^2D}{4a_i}|=\frac{Db_i^2}{4}`$. Thus $`a_{i+1}<\sqrt{D}/2`$ and corollary 1 implies that $`I_{i+1}`$ is reduced.
Finally we prove the statement about $`\alpha `$. Clearly $`I_{\mathrm{red}}=I/\alpha `$ and since $`I_{\mathrm{red}}`$ is reduced it follows that $`\alpha `$ must be a minimum in $`I`$. To see this think of the geometrical picture: we have $`I=\alpha I_{\mathrm{red}}`$ so the lattice for $`I`$ is obtained from the lattice for $`I_{\mathrm{red}}`$ by rescaling by $`\alpha `$ and $`\overline{\alpha }`$ in the $`x`$ and $`y`$ directions respectively. Hence the interior of the $`\widehat{\alpha }`$ rectangle for $`I`$ corresponds to the interior of the $`\widehat{1}`$ rectangle for $`I_{\mathrm{red}}`$ which is empty except for the point $`(0,0)`$.

The right neighbour of a minimum $`\alpha I`$ is the uniquely determined minimum $`\beta _RI`$ with least size $`\beta _R>\alpha `$. The left neighbour of a minimum $`\alpha I`$ is the uniquely determined minimum $`\beta _LI`$ with least conjugate size $`|\overline{\beta _L}|>|\overline{\alpha }|`$.

###### Proposition 22

Let $`\alpha [\sqrt{d}]`$ with $`\alpha >0`$. Then for any fractional ideal $`I`$ the mapping $`I\alpha I`$ is a bijection mapping minima to minima and left (resp. right) neighbours to left (resp. right) neighbours.

Proof This is immediate from the geometrical picture of multiplication by $`\alpha `$ as just rescaling the lattice by $`\alpha `$ and $`\overline{\alpha }`$ in the $`x`$ and $`y`$ directions respectively.

###### Proposition 23

If $`I=+\gamma (I)`$ is reduced then $`\gamma (I)>1`$ and $`1<\overline{\gamma (I)}<0`$.

Proof By proposition 20 if $`I`$ is reduced then $`\gamma (I)=(b+\sqrt{D})/(2a)>1`$. Also by proposition 19 $`a<\sqrt{D}`$ so $`\sqrt{D}2a<b<\sqrt{D}`$ so $`1<(b\sqrt{D})/(2a)<0`$.

###### Proposition 24

If $`I=+\gamma (I)`$ is reduced then $`\gamma (I)`$ is a minimum of $`I`$ and $`\rho (I)`$ is again reduced.

Proof Write $`\gamma =\gamma (I)`$. To see that it is a minimum of $`I`$ we need to show that if $`x+y\gamma `$ (with $`x,y`$) has $`|x+y\gamma |<|\gamma |`$ and $`|x+y\overline{\gamma }|<|\overline{\gamma }|`$ then $`x=y=0`$. Without loss of generality we may assume $`x0`$. Suppose the above conditions hold for $`x,y`$. Since $`x0`$, $`\gamma >1`$ and $`\gamma <x+y\gamma <\gamma `$ we see that $`y0`$. Write $`\xi =|\overline{\gamma }|`$ so $`0<\xi <1`$ and $`|x+y\overline{\gamma }|<|\overline{\gamma }|`$ gives $`\xi <xy\xi <\xi `$. Since $`x0`$ we get $`y\xi <\xi `$ so $`y>1`$. But we had $`y0`$ so $`y=0`$ is the only possibility. Then $`|x|<|\xi |<1`$ gives $`x=0`$. Thus $`\gamma `$ is a minimum of $`I`$ and by proposition 22 $`\rho (I)`$ is again a reduced ideal.

###### Proposition 25

Let $`I=+\gamma (I)`$ be a reduced ideal. Then the minimum $`\gamma (I)I`$ is the right neighbour of 1 in $`I`$.

Proof Suppose that $`\alpha =x+y\gamma (I)`$ is the right neighbour of 1. We first show that $`y>0`$. If $`y=0`$ then $`\alpha =x`$ so $`x>1`$ i.e. $`\alpha 2`$, $`\alpha =\overline{\alpha }`$ and the $`\widehat{\alpha }`$ rectangle contains $`(1,1)`$ i.e. $`\alpha `$ cannot be a minimum.
If $`y<0`$ put $`y=k`$ for $`k>0`$. Then $`\alpha =xk\gamma >1`$ and $`\gamma >1`$ gives $`x>1+k\gamma >1+k`$. Then $`\overline{\alpha }=xk\overline{\gamma }>1+kk\overline{\gamma }>1+k`$ (as $`\overline{\gamma }<0`$). Hence again the $`\widehat{\alpha }`$ rectangle contains $`(1,1)`$ so $`\alpha `$ cannot be a minimum. Thus $`y>0`$. Next we show that $`x0`$. If $`x<0`$ put $`x=k`$ for $`k>0`$. Then $`\alpha =k+y\gamma >1`$ and $`\overline{\alpha }=k+\overline{\gamma }<k1`$ i.e. $`\overline{\alpha }<1`$ and the $`\widehat{\alpha }`$ rectangle contains $`(1,1)`$ i.e. $`\alpha `$ cannot be a minimum. Hence $`\alpha =x+y\gamma `$ with $`x0`$ and $`y>0`$ are the only possibilities and the smallest such $`\alpha >1`$ is $`\alpha =\gamma `$ which actually is a minimum by proposition 24.

### 6.3 The principal cycle of reduced ideals

Since $`𝒪=+\frac{D+\sqrt{D}}{2}=+\frac{\tau (D,2)+\sqrt{D}}{2}`$ we see from proposition 20 that $`𝒪`$ is a reduced principal ideal of $`𝒪`$. Thus $`\alpha _0=1𝒪`$ is a minimum and for $`i`$ let $`\alpha _{i1}`$ be the left neighbour and $`\alpha _{i+1}`$ the right neighbour of the minimum $`\alpha _i𝒪`$. Also let $`J_i=\frac{1}{\alpha _i}𝒪=+\gamma _i`$. (Note that since $`\alpha _i`$ is a minimum of $`𝒪`$, proposition 22 shows that $`1J_i`$ is a minimum so $`J_i`$ is reduced and hence by proposition 18 has the form $`+\gamma _i`$.)

Since $`\alpha _{i+1}`$ is the right neighbour of $`\alpha _i`$ in $`𝒪`$ it follows that $`\alpha _{i+1}/\alpha _i`$ is the right neighbour of 1 in $`\frac{1}{\alpha _i}𝒪`$ and proposition 25 gives $`\alpha _{i+1}/\alpha _i=\gamma _i`$ i.e.

$$\alpha _{i+1}=\alpha _i\gamma _i\text{and }J_{i+1}=\rho (J_i)\text{.}$$

Remark (Geometrical picture of the sequence of minima) If we plot the points $`\widehat{\alpha }_i^2`$ they all lie in the right half of the plane ($`\alpha _i>0`$) on a pair of hyperbola–like curves (like $`y=\pm 1/x`$) alternating with $`\pm y`$ values as $`i`$ varies. To see this recall that $`\alpha _{i+1}>\alpha _i`$ (by definition) and so $`|\overline{\alpha }_{i+1}|<|\overline{\alpha }_i|`$ (since $`|\overline{\alpha }_{i+1}|>|\overline{\alpha }_i|`$ would imply that $`\widehat{\alpha }_i`$ is inside the $`\widehat{\alpha }_{i+1}`$ rectangle) i.e. the sequence $`\{|\overline{\alpha }_i|\}`$ is monotonically decreasing with increasing $`i`$. To see that $`\overline{\alpha }_i`$ and $`\overline{\alpha }_{i+1}`$ have opposite signs consider $`J_i=𝒪/\alpha _i=+\gamma _i`$. As noted above we have $`\alpha _{i+1}/\alpha _i=\gamma _i`$ and proposition 23 gives $`1<\overline{\gamma }_i<0`$ so $`\overline{\alpha }_{i+1}/\overline{\alpha }_i<0`$.

###### Proposition 26

For all $`i`$

$$\mathrm{ln}\alpha _{i+1}\mathrm{ln}\alpha _i\frac{1}{2}\mathrm{ln}D$$

Proof We have $`\alpha _{i+1}/\alpha _i=\gamma _i`$ where $`J_i=+\gamma _i`$ is a reduced ideal and $`\gamma _i=\frac{b_i+\sqrt{D}}{2a_i}`$. Since $`J_i`$ is reduced, proposition 19 gives $`b_i<\sqrt{D}`$ so $`\gamma _i<\frac{2\sqrt{D}}{2a_i}<\sqrt{D}`$ giving $`\mathrm{ln}(\alpha _{i+1}/\alpha _i)\mathrm{ln}\sqrt{D}`$.

###### Proposition 27

For all $`i`$

$$\mathrm{ln}\alpha _{i+1}\mathrm{ln}\alpha _i\mathrm{ln}\left(1+\frac{3}{16D}\right)\frac{3}{32D}.$$

(Remark: In  (at eq. (5.4)) a stronger lower bound of $`\mathrm{ln}(1+1/\sqrt{D})`$ is claimed but the above will suffice for our purposes.)

Proof As in the proof of proposition 26 we have

$$\frac{\alpha _{i+1}}{\alpha _i}=\gamma _i=\frac{b_i+\sqrt{D}}{2a_i}>1.$$

Omitting the subscripts $`i`$ we have $`\frac{b+\sqrt{D}}{2a}=1+\frac{b+\sqrt{D}2a}{2a}`$. Let $`ϵ=\sqrt{D}\sqrt{D}`$ and $`\xi =\frac{b+\sqrt{D}2a}{2a}=\frac{\sqrt{D}(2ab)}{2a}`$. Then since $`0<a<\sqrt{D}`$ and $`\xi >0`$ with $`2ab`$ we have

$$\xi >\frac{ϵ}{2\sqrt{D}}.$$

Write $`K=\sqrt{D}`$ and $`D=K^2+L`$ with $`1<L<2K+1<K^2`$ so $`\sqrt{D}=K\sqrt{1+L/K^2}`$. We apply the binomial inequality

$$1+x/2>\sqrt{1+x}>1+x/2x^2/8$$

and for $`0<x<1`$ we have $`x^2<x`$ so $`\sqrt{1+x}>1+3x/8`$. Hence

$$\sqrt{D}>K+\frac{3L}{8K}>K+\frac{3}{8K}>K+\frac{3}{8\sqrt{D}}$$

as $`K<\sqrt{D}`$. Thus $`ϵ>\frac{3}{8\sqrt{D}}`$ and $`\xi >\frac{3}{16D}`$ so $`\alpha _{i+1}/\alpha _i>1+\frac{3}{16D}`$. Finally using (for $`0<x<1`$)

$$\mathrm{ln}(1+x)>xx^2/2>xx/2=x/2$$

we get $`\mathrm{ln}(\alpha _{i+1}/\alpha _i)>\mathrm{ln}(1+\frac{3}{16D}>\frac{3}{32D}`$.

Thus we have upper and lower bounds on the separation between consecutive $`\mathrm{ln}\alpha _i`$ values. However the lower bound is exponentially small (in $`\mathrm{log}D`$) and we next give a constant lower bound for the separation between every second member of the sequence.

###### Proposition 28

For all $`i`$

$$\mathrm{ln}\alpha _{i+1}\mathrm{ln}\alpha _{i1}\mathrm{ln}2.$$

Proof We saw previously that $`\overline{\alpha }_{i1}`$ and $`\overline{\alpha }_{i+1}`$ have the same sign and the sequence $`|\overline{\alpha }_i|`$ is strictly decreasing with $`i`$. Hence $`|\overline{\alpha }_{i+1}\overline{\alpha }_{i1}|<|\overline{\alpha }_{i1}|`$. Now if $`\alpha _{i+1}/\alpha _{i1}<2`$ the we would have $`\alpha _{i1}<\alpha _{i+1}<2\alpha _{i1}`$ so $`0<\alpha _{i+1}\alpha _{i1}<\alpha _{i1}`$ i.e. if we write $`\beta =\alpha _{i+1}\alpha _{i1}`$ then $`\widehat{\beta }`$ lies inside the $`\widehat{\alpha }_{i1}`$-rectangle. Since $`\alpha _{i\pm 1}𝒪`$ we have $`\beta 𝒪`$ and this contradicts the minimality of $`\alpha _{i1}`$. Hence $`\alpha _{i+1}/\alpha _{i1}2`$.

###### Proposition 29

The sequence $`\{\alpha _i\}`$ contains all the minima of $`𝒪`$.

Proof Let $`\alpha 𝒪`$ be any minimum. From propositions 26 and 28 we see that $`lim_{i\pm \mathrm{}}\alpha _i=\pm \mathrm{}`$ so there must be an $`i`$ with $`\alpha _i\alpha <\alpha _{i+1}`$. We claim $`\alpha =\alpha _i`$. Otherwise $`\alpha >\alpha _i`$ contradicting the definition that $`\alpha _{i+1}`$ is the minimum with least size greater than $`\alpha _i`$.

###### Theorem 4

(The principal cycle of reduced ideals).
(a) The sequence $`\{J_i\}_i`$ is periodic i.e. there is (a smallest) $`k_0`$ such that $`J_i=J_j`$ iff $`ij\mathrm{mod}k_0`$ for all $`i,j`$. The repeating segment $`\{J_0=𝒪,J_1,\mathrm{}J_{k_01}\}`$ of principal reduced ideals is called the principal cycle.
(b) Let $`ϵ=\alpha _{k_0}/\alpha _0=\alpha _{k_0}`$. Then $`ϵ=ϵ_0`$, the fundamental unit of $`𝒪`$.
(c) Let $`I`$ be any reduced principal fractional ideal. Then $`I`$ is in the principal cycle.

Proof (a) By proposition 19 the sequence $`\{J_i\}_i`$ contains only finitely many different ideals. Hence for some $`i,k`$ we have $`J_i=J_{i+k}`$ so $`\alpha _i𝒪=\alpha _{i+k}𝒪`$. Write $`\eta =\alpha _{i+k}/\alpha _i`$. Since $`\alpha _{i+k}=\eta \alpha _i`$ it follows from proposition 22 that $`\alpha _{s+k}=\eta \alpha _s`$ for any $`s`$ and $`J_s=J_{s+k}=J_{s+lk}`$ for all $`l,s`$. Set $`s=0`$ and choose the minimal $`k_0`$ such that $`𝒪=J_{k_0}=\alpha _{k_0}𝒪`$. Write $`ϵ=\alpha _{k_0}`$ which is necessarily a unit of $`𝒪`$ by proposition 10. Then $`J_i`$ for $`0i<k_0`$ are pairwise distinct and

$$J_s=J_t\text{iff }st\mathrm{mod}k_0\text{ for }s,t\text{ iff }\alpha _s=\alpha _tϵ^l\text{ for some }l$$
(11)
(b) To see that $`ϵ=\alpha _{k_0}`$ is the fundamental unit $`ϵ_0`$ of $`𝒪`$ let $`\eta `$ be any unit with $`\eta >0`$. Then $`\eta `$ is a minimum of $`𝒪`$ (because if $`\xi 𝒪`$ has $`|\xi |<|\eta |`$ and $`|\overline{\xi }|<|\overline{\eta }|`$ then $`\xi /\eta 𝒪`$ has $`|\xi /\eta |<1`$ and $`|\overline{\xi /\eta }|=|\overline{\xi }/\overline{\eta }|<1`$ contradicting the minimality of 1 in $`𝒪`$). Hence by proposition 29 $`\eta 𝒪=J_k`$ for some $`k`$. But $`\eta 𝒪=𝒪`$ if $`\eta `$ is a unit so eq. (11) gives $`\eta =ϵ^l`$ for some $`l`$. Hence $`ϵ=ϵ_0`$ the fundamental unit.
(c) Since $`I`$ is principal we can write $`I=\frac{1}{\alpha }𝒪`$. Hence $`𝒪=\alpha I`$ and since 1 is a minimum of $`I`$ proposition 22 shows that $`\alpha `$ is a minimum of $`𝒪`$. Then proposition 29 shows that $`I=J_k`$ for some $`k`$.

###### Proposition 30

The length $`k_0`$ of the principal cycle satisfies

$$\frac{2R}{\mathrm{ln}D}k_0\frac{2R}{\mathrm{ln}2}.$$

Proof We have $`R=\mathrm{ln}ϵ_0=\mathrm{ln}\alpha _{k_0}=\mathrm{ln}\alpha _{k_0}\mathrm{ln}\alpha _0`$ (as $`\alpha _0=1`$). Then proposition 28 gives $`\frac{k_0}{2}\mathrm{ln}2R`$ i.e $`k_02R/\mathrm{ln}2`$. Similarly proposition 26 gives $`k_0(\frac{1}{2}\mathrm{ln}D)R`$.

### 6.4 The inverse of the reduction operator

Recall that if we apply the reduction operator $`\rho `$ sufficiently many times to $`I=+\frac{b+\sqrt{D}}{2a}`$ we will always eventually obtain a reduced ideal. As $`a>0`$ can be arbitrarily large, there are infinitely many distinct ideals of this form. We also know that there are only finitely many reduced ideals and if $`I`$ is reduced then $`\rho (I)`$ is reduced too. Hence as a mapping on general ideals $`\rho `$ cannot be one-to-one.

However if we restrict to the subset of reduced principal ideals i.e. the principal cycle $`𝒫_{\mathrm{red}}=\{J_0=𝒪,J_1,\mathrm{},J_{k_01}\}`$ then we have $`J_{i+1}=\rho (J_i)`$ (cycling with $`\rho (J_{k_01})=J_0`$) so $`\rho `$ is invertible and we now develop an explicit expression for the inverse map $`\rho ^1`$. Then together with the formula for the action of $`\rho `$ (in eq. (10)) we will be able to step in either direction along the principal cycle.

Let $`I=+\frac{b+\sqrt{D}}{2a}=+\gamma `$ be any reduced ideal. We define the conjugated ideal

$$\sigma (I)=\overline{I}=+\frac{b\sqrt{D}}{2a}=+\frac{\tau (b,a)+\sqrt{D}}{2a}$$

where the last expression (using $`=`$) is in standard form. In terms of the geometrical picture of ideals, with $`\alpha I`$ embedded in $`^2`$ as $`\widehat{\alpha }=(\alpha ,\overline{\alpha })`$, we see that the conjugation operation simply reflects the lattice in the $`45^{}`$ line $`y=x`$. Then the following facts are immediately evident:
(i) If $`I`$ is reduced then $`\overline{I}`$ is reduced (i.e. 1 stays a minimum under conjugation).
(ii) If $`\alpha `$ is a minimum in $`I`$ then $`|\overline{\alpha }|`$ is a minimum in $`\overline{I}`$.
(iii) If $`\alpha `$ is the left (right) neighbour of a minimum $`\beta `$ in $`I`$ then $`|\overline{\alpha }|`$ is the right (left) neighbour of the minimum $`|\overline{\beta }|`$ in $`\overline{I}`$.

Now write $`b_{}=\tau (b,a)`$. Then since $`\delta =\frac{\tau (b,a)+\sqrt{D}}{2a}=\frac{b_{}+\sqrt{D}}{2a}`$ is the right neighbour of 1 in the reduced ideal $`\overline{I}`$ (cf. proposition 25), we see that

$$|\overline{\delta }|=\left|\frac{b_{}\sqrt{D}}{2a}\right|=\frac{\sqrt{D}b_{}}{2a}$$

is the left neighbour of 1 in $`I`$. Hence by the definition of $`\rho (I)=\frac{1}{\gamma }I`$ we get

$$\rho ^1(I)=\frac{1}{|\overline{\delta }|}I=\frac{2a}{\sqrt{D}b_{}}\left(+\frac{b+\sqrt{D}}{2a}\right).$$

To express this in standard form note that $`b`$ and $`b_{}`$ differ by a multiple of $`2a`$ and $`=`$ so we get $`+\frac{b+\sqrt{D}}{2a}=+\frac{b_{}\sqrt{D}}{2a}`$ so

$$\rho ^1(I)=+\frac{2a}{\sqrt{D}b_{}}=+\frac{b_{}+\sqrt{D}}{2c_{}}$$

where $`c_{}=(Db_{}^2)/(4a)`$. Hence if $`\rho ^1(I)=+\frac{b^{\prime \prime }+\sqrt{D}}{2a^{\prime \prime }}`$ is the standard form, we have the explicit formulae:

$$b_{}=\tau (b,a)a^{\prime \prime }=\frac{Db_{}^2}{4a}b^{\prime \prime }=\tau (b_{},a^{\prime \prime }).$$
(12)
Remark From the geometrical picture we also see that $`\rho ^1(I)=\sigma \rho \sigma (I)`$ and above we saw that $`\sigma `$ induces the mapping $`aa`$, $`bb_{}`$. These formulae together with eq. (10) may also be used to derive the above expressions for $`\rho ^1`$.

Since $`0<a,b<\sqrt{D}`$ for reduced ideals we see from eqs. (10) and (12) that the action of $`\rho `$ and $`\rho ^1`$ may be computed in $`\mathrm{poly}\mathrm{log}D`$ time.

## 7 The distance function for ideals

Let $`I_1`$ and $`I_2`$ be fractional (principal) ideals of $`𝒪`$ which are related by $`I_1=\gamma I_2`$ for some $`\gamma [\sqrt{d}]`$. Then the distance $`\delta (I_1,I_2)`$ is defined by

$$\delta (I_1,I_2)=\mathrm{ln}|\gamma |\mathrm{mod}R$$
(13)
(recalling that $`R=\mathrm{ln}ϵ_0`$ where $`ϵ_0`$ is the fundamental unit of $`[\sqrt{d}]`$). If $`I_1\gamma I_2`$ for any $`\gamma [\sqrt{d}]`$ then the distance is not defined.

Note first that although $`\gamma `$ is not unique, $`\delta (I_1,I_2)`$ is well defined: if $`I_1=\gamma ^{}I_2`$ as well as $`I_1=\gamma I_2`$ then by proposition 10 we must have $`\gamma ^{}=ϵ\gamma =ϵ_0^k\gamma `$ for some $`k`$. Hence $`\mathrm{ln}\gamma ^{}=\mathrm{ln}\gamma +kR`$. Also from eq. (13) we have $`\delta (I_1,I_2)=\delta (I_2,I_1)`$ (when either is defined).

Of particular interest will be the distance $`\delta (𝒪,I)`$ of any principal ideal from the unit ideal $`𝒪`$. We write $`\delta (I)`$ for $`\delta (𝒪,I)`$.

Now recall the principal cycle of reduced ideals $`J_i=𝒪/\alpha _i=+\gamma _i`$ with $`i=0,1,\mathrm{},k_01`$. Thus $`\delta (J_i)=\mathrm{ln}\alpha _i`$ and $`\delta (J_i,J_k)=\mathrm{ln}\alpha _k\mathrm{ln}\alpha _i`$. We also had $`J_{i+1}=\rho (J_i)`$ and $`\alpha _{i+1}=\gamma _i\alpha _i`$. Then propositions 26, 27 and 28 immediately give the following.

###### Proposition 31

For all $`i`$

$$\frac{3}{32D}\delta (J_i,\rho (J_i))=\mathrm{ln}\gamma _i\frac{1}{2}\mathrm{ln}D.$$

###### Proposition 32

For all $`i`$

$$\delta (J_i,\rho ^2(J_i))\mathrm{ln}2.$$

Furthermore for principal ideals that are not reduced we show that the reduction process of proposition 21 leads to a reduced ideal $`I_{\mathrm{red}}`$ that is close in distance to $`I`$.

###### Proposition 33

Let $`I=+\frac{b+\sqrt{D}}{2a}`$ be any (not necessarily reduced) principal fractional ideal. Let us place ideals along $``$ at positions corresponding to their distances from $`𝒪`$ at 0. In the notation of proposition 21 let $`i_{\mathrm{red}}`$ be the least integer such that $`I_{\mathrm{red}}=\rho ^{i_{\mathrm{red}}}(I)=\frac{1}{\alpha }I`$ is reduced. Let $`J_k`$ be the nearest member of the principal cycle to $`I`$ with the property that $`\overline{\alpha }_k`$ has opposite sign to $`\overline{\alpha }`$ (recalling that the $`\overline{\alpha }_j`$’s alternate in sign).
Then $`I`$ lies between $`J_{k1}`$ and $`J_{k+1}`$ and $`I_{\mathrm{red}}`$ is one of $`J_{k1},J_k,J_{k+1}`$. Thus also

$$|\delta (I,I_{\mathrm{red}})|<\mathrm{ln}D$$

and $`\delta (\rho ^2(I_{\mathrm{red}})>\delta (I)`$.

Proof Recall that $`\alpha `$ is a minimum of $`I`$. The fact that $`I`$ lies between $`J_{k1}`$ and $`J_{k+1}`$ with $`J_k`$ as above, and that $`I_{\mathrm{red}}`$ is one of $`J_{k1},J_k,J_{k+1}`$, follows from the geometrical picture of ideals as lattices in $`^2`$ (as claimed in ).
——
I can’t yet quite see how this works and proofs from readers – to richard@cs.bris.ac.uk – would be most welcome!
——
Given these facts proposition 31 gives $`|\delta (I,I_{\mathrm{red}})|<\mathrm{ln}D`$ and since $`\rho `$ cycles consecutively through the principal cycle we get $`\delta (\rho ^2(I_{\mathrm{red}})>\delta (I)`$.

### 7.1 Products of ideals – making large distance jumps

We will need an efficient method of locating ideals (and their distances) that are far out along the exponentially long principal cycle. Of course applying $`\rho `$ repeatedly to $`𝒪`$ will eventually reach any ideal on the cycle (and we can accumulate the successive distance increments too) but in view of proposition 31, for exponentially distant ideals this will require exponentially many steps. Hence we introduce a method of multiplying ideals together (with corresponding addition of distances) which will allow large distance jumps via iterated squaring.

If $`I_1`$ and $`I_2`$ are ideals then the product $`I_1I_2`$ is defined to be the $``$-linear span of the set $`\{\alpha \beta :\alpha I_1,\beta I_2\}`$. $`I_1I_2`$ is clearly again an ideal. If $`\{\alpha _1,\alpha _2\}`$ and $`\{\beta _1,\beta _2\}`$ are integral bases of $`I_1`$ and $`I_2`$ respectively then $`I_1I_2`$ is the $``$-linear span of $`\{\alpha _1\beta _1,\alpha _1\beta _2,\alpha _2\beta _1,\alpha _2\beta _2\}`$. For principal ideals given as $`I_1=\xi _1𝒪`$ and $`I_2=\xi _2𝒪`$ we simply have $`I_1I_2=\xi _1\xi _2𝒪`$ but we will be interested in computing products of (reduced) ideals in the presentation of eq. (8).

###### Proposition 34

Let

$$I_i=a_i+\frac{b_i+\sqrt{D}}{2}i=1,2$$

be principal ideals. Let $`k=\mathrm{gcd}(a_1,a_2,(b_1+b_2)/2)`$ and let $`u,v,w`$ be integers such that

$$ua_1+va_2+w(b_1+b_2)/2=k$$

(which are guaranteed to exist by Euclid’s algorithm). Then

$$I_3=I_1I_2=k\left(a_3+\frac{b_3+\sqrt{D}}{2}\right)$$

where

$$a_3=a_1a_2/k^2\text{and}b_3=\tau ((ua_1b_2+va_2b_1+w\frac{(b_1b_2+D)}{2})/k,a_3).$$

Proof Let $`k(a_3+\frac{b_3+\sqrt{D}}{2})`$ be the standard representation of $`I_3`$ (as in proposition 15). For principal ideals $`I=\xi 𝒪`$ we had $`𝒩(I)=|\xi \overline{\xi }|`$ (cf. proposition 13) so $`𝒩(I_3)=𝒩(I_1)𝒩(I_2)`$. Then using the formula for $`𝒩(I)`$ in proposition 15 we get $`k^2a_3=a_1a_2`$ so $`a_3=a_1a_2/k^2`$. To derive the claimed formula for $`k`$ note first that (cf. proposition 15) $`b_1^2D`$ is a multiple of 4 so $`b_1^2D\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$. Hence $`b_1D\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$ (as integers and their squares always have the same even/odd parity). Similarly $`b_2D\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$ so $`b_1b_2\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}2}`$ and $`(b_1+b_2)/2`$ is an integer. Next recall that $`k/2`$ is uniquely determined as the least positive coefficient of $`\sqrt{D}`$ in any member of $`I_3`$. But $`I_3`$ is generated over $``$ by $`a_1a_2,a_1(b_2+\sqrt{D})/2,a_2(b_1+\sqrt{D})/2`$ and $`(b_1b_2+(b_1+b_2)\sqrt{D}+D)/4`$ so the allowable coefficients of $`\sqrt{D}`$ have the form $`\frac{1}{2}(xa_1+ya_2+z(b_1+b_2)/2)`$ with $`x,y,z`$. Hence Euclid’s algorithm gives $`k=\mathrm{gcd}(a_1,a_2,(b_1+b_2)/2)`$. Write

$$k=ua_1+va_2+w(b_1+b_2)/2\text{for }u,v,w\text{.}$$

Then we must have

$$k\left(\frac{b_3+\sqrt{D}}{2}\right)=ua_1\frac{b_2+\sqrt{D}}{2}+va_2\frac{b_1+\sqrt{D}}{2}+w\frac{b_1b_2+(b_1+b_2)\sqrt{D}+D}{4}+sa_1a_2$$

for some $`s`$. Equating coefficients of the $`\sqrt{D}`$-free terms we get

$$b_3=\frac{1}{k}\left(ua_1b_2+va_2b_1+w(b_1b_2+D)/2\right)+2sa_1a_2/k.$$

Since $`a_1a_2/k=ka_3`$ the final term is an even multiple of $`a_3`$ and the uniqueness condition $`b_3=\tau (b_3,a_3)`$ gives the claimed formula.

It follows immediately from the definitions that

$$\delta (I_1I_2)=\delta (I_1)+\delta (I_2)$$

if we do not reduce the sum of the distances $`\mathrm{mod}R`$. We also point out that even if $`I_1`$ and $`I_2`$ are reduced then $`I_1I_2`$ is generally not reduced.

We will use products primarily in the special case that $`I_1=I_2=I`$ where

$$I=+\frac{b+\sqrt{D}}{2a}=\frac{1}{a}\left(a+\frac{b+\sqrt{D}}{2}\right)$$

is a reduced ideal in the principal cycle. In that case proposition 34 gives

$$I_2=II=\frac{k^{}}{a^{}}\left(a^{}+\frac{b^{}+\sqrt{D}}{2}\right)$$

with

$$k^{}=\mathrm{gcd}(a,b)=ua+wba^{}=a^2/k^2b^{}=\tau ((ua+w(b^2+D)/2)/k^{},a^{})$$

so

$$I_2=\frac{1}{k^{}}\left(+\frac{b^{}+\sqrt{D}}{2a^{}}\right)\text{and }\delta (I_2)=2\delta (I)\text{.}$$

Here $`a^{},b^{},k^{}`$ are unique and explicitly calculated so we can consider the ideal

$$I_2^{}=+\frac{b^{}+\sqrt{D}}{2a^{}}$$

which is generally not reduced. Since $`I`$ was reduced we have $`0<a,b<\sqrt{D}`$ and so $`k^{}=\mathrm{gcd}(a,b)<\sqrt{D}`$. Hence $`|\delta (I_2,I_2^{})|=\mathrm{ln}k^{}<\frac{1}{2}\mathrm{ln}D`$. Next use proposition 21 to obtain a reduced ideal $`I_2^{\prime \prime }`$ from $`I_2^{}`$, which will be a member of the principal cycle (by theorem 4 (c)). Furthermore by proposition 33 we have $`|\delta (I_2^{},I_2^{\prime \prime })|<\mathrm{ln}D`$ so $`|\delta (I_2,I_2^{\prime \prime })|<\frac{3}{2}\mathrm{ln}D`$. Hence in view of proposition 32 if we apply $`\rho `$ or $`\rho ^1`$ to $`I_2^{\prime \prime }`$, $`2n`$ times where $`n<(\frac{3}{2}\mathrm{ln}D)/\mathrm{ln}2=O(\mathrm{ln}D)`$, then we can locate the first member $`J_k`$ of the principal cycle with distance $`\delta (J_k)>2\delta (I)`$ (and also compute its distance $`\delta (J_k)`$). We denote this uniquely determined ideal $`J_k`$ by $`II`$ i.e. for any member $`I`$ of the principal cycle, $`II`$ is the first member with distance exceeding twice the distance of $`I`$. (In the above construction if $`2\delta (I)`$ exceeds $`R`$ then we simply wrap around the principal cycle, passing through $`𝒪`$ again.)

Let us now estimate the computational complexity of computing $`II`$ from $`I`$. Since $`I`$ was reduced we have $`a,b=O(\sqrt{D})`$. Also in $`k^{}=ua+wb`$ the integers $`u,w`$ are not unique and we have the freedom (leaving $`k^{}`$ unchanged):

$$uuxbww+xa\text{for any }x\text{.}$$

Hence we can always make $`u<b=O(\sqrt{D})`$ and $`k^{}=ua+wb`$ gives $`w=O(D)`$ too. Thus $`a^{},b^{},k^{}`$ are all at most $`O(D^2)`$ and the arithmetic calculation of $`I_2^{}`$ can be performed in $`O(\mathrm{poly}\mathrm{log}D)`$ time. Furthermore the reduction operation of proposition 21 requires $`O(\mathrm{log}(a^{}/\sqrt{D}))`$ steps for $`a^{}=O(D^2)`$ and to compute $`I_2^{\prime \prime }=II`$ we apply $`\rho ^2`$ or $`\rho ^2`$ at most $`(\frac{3}{2}\mathrm{ln}D)/\mathrm{ln}2=O(\mathrm{ln}D)`$ times. Hence the entire computation of $`II`$ from $`I`$ can be performed in $`O(\mathrm{poly}\mathrm{log}D)`$ time and we have proved the following.

###### Proposition 35

Let $`I`$ be a reduced principal ideal. Consider the iterated $``$squaring:

$$II^{(2)}=III^{(4)}=I^{(2)}I^{(2)}\mathrm{}I^{(2^n)}=I^{(2^{n1})}I^{(2^{n1})}.$$

The final ideal $`I^{(2^n)}`$ has distance $`\delta (I^{(2^n)})>2^n\delta (I)`$ (where we have not reduced the growing distances $`\mathrm{mod}R`$) and this ideal (with its unreduced distance) can be computed in $`O(\mathrm{poly}(\mathrm{log}D),n)`$ time.

More generally if $`I_1`$ and $`I_2`$ are reduced ideals

$$I_i=+\frac{b_i+\sqrt{D}}{2a_i}$$

(so $`0<a_i,b_i<\sqrt{D}`$) we define $`I_1I_2`$ to be the first member of the principal cycle whose distance exceeds $`\delta (I_1)+\delta (I_2)`$. Then following the methods above it is easy to see that $`I_1I_2`$ (i.e. its $`a,b`$ parameters) can be computed in $`\mathrm{poly}\mathrm{log}D`$ time from $`a_1,a_2,b_1,b_2`$.

## 8 Summary – the picture so far

Given $`d`$ we have the algebraic integers $`𝒪[\sqrt{d}]`$ and a finite (but exponentially large in $`\mathrm{log}d`$) set of principal reduced ideals $`\{J_0=𝒪,J_1,\mathrm{},J_{k_01}\}`$ of $`𝒪`$. Each $`J_i`$ is defined by a pair of integers $`a,b`$ with $`0a,b<\sqrt{D}`$ and hence has a $`\mathrm{poly}\mathrm{log}d`$ sized description. We write $`J_i=+\gamma _i=+\frac{b_i+\sqrt{D}}{2a_i}`$.

The reduced ideals $`J_i`$ can be placed around a circle of circumference $`R=\mathrm{ln}ϵ_0`$ at irregularly spaced distances $`\delta (J_i)`$ from the ideal $`𝒪`$ at distance 0, giving the so-called principal cycle. Since $`J_i`$ is principal it has the form $`J_i=\alpha _i𝒪`$ for some $`\alpha _i[\sqrt{d}]`$ and then $`\delta (J_i)=\mathrm{ln}|\alpha _i|\mathrm{mod}R`$.

We have a reduction operator $`\rho `$ such that $`J_{i+1}=\rho (J_i)`$ and $`\rho `$ and $`\rho ^1`$ are polynomial time computable mappings allowing us to step either direction around the principal cycle.

We have an (exponentially small) lower bound $`d_{\mathrm{min}}=3/(32D)`$ on the distance between consecutive ideals $`J_i`$ and an upper bound of $`\frac{1}{2}\mathrm{ln}D`$. We also have a constant lower bound of $`\mathrm{ln}2`$ on the distance between $`J_i`$ and $`J_{i+2}`$.

If $`J_i`$ is given in the form $`+\gamma _i`$ then we cannot compute $`\delta (J_i)`$ in $`\mathrm{poly}\mathrm{log}d`$ time (e.g. we cannot translate $`+\gamma _i`$ efficiently to the $`\alpha _i𝒪`$ description). But we can efficiently compute the distance increments induced by $`\rho `$ and obtain the distance increment from $`J_i`$ to $`J_{i+1}`$ in time $`\mathrm{poly}\mathrm{log}d`$ if $`\gamma _i`$ is given.

The above method does not allow us to efficiently move far out along the principal cycle starting from $`𝒪`$ (as this would require exponentially many steps). To achieve such large jumps we introduced a product operation $`IJ`$ on reduced ideals $`I,J`$ with the property that $`IJ`$ is the first reduced ideal having distance exceeding $`\delta (I)+\delta (J)`$. $`IJ`$ and $`\delta (IJ)`$ are then efficiently computable so by iterated $``$squaring of $`J_2=\rho ^2(𝒪)`$ having $`\delta (J_2)>\mathrm{ln}2`$ we can move out to a distance exceeding $`2^n\mathrm{ln}2`$ in $`\mathrm{poly}(\mathrm{log}d,n)`$ time. If $`2^n\mathrm{ln}2`$ exceeds $`R`$ as $`n`$ increases, the increasingly distant ideals simply wrap around the circle while the distance we compute is not reduced $`\mathrm{mod}R`$.

## 9 The periodic function for Hallgren’s algorithm

Recall that $`𝒫_{\mathrm{red}}`$ denotes the (finite) set of all reduced principal fractional ideals i.e. the principal cycle, and $`\delta (I)`$ denotes the distance of $`I`$ from the unit ideal $`𝒪`$. Define $`h:𝒫_{\mathrm{red}}\times `$ as follows. Let $`\stackrel{~}{x}x\mathrm{mod}R`$ with $`0\stackrel{~}{x}<R`$. Then

$$h(x)=(I_x,\stackrel{~}{x}\delta (I_x))$$

where $`I_x𝒫_{\mathrm{red}}`$ is the ideal having greatest distance $`\delta (I_x)<\stackrel{~}{x}`$. In other words, if we place ideals $`I`$ along the real axis at positions in $`[0,R)`$ given by their distances $`\delta (I)`$ from $`𝒪`$ at $`x=0`$, and periodically reproduce this pattern in each interval of length $`R`$, then $`I_x`$ is the ideal that is nearest to the left of $`x`$ and $`\stackrel{~}{x}\delta (I_x)`$ is the distance gap up to $`x`$.

Remark Intuitively we simply wanted $`h^{}(x)=I_x`$ but we include the information of the positive distance gap $`\stackrel{~}{x}\delta (I_x)`$ in the value of $`h(x)`$ to ensure that $`h`$ is a one-to-one function within each period (noting that $`h^{}`$ is constant for $`x`$ varying between consecutive ideals).

The main point of our whole development of the theory of reduced ideals is to prove the following result.

###### Theorem 5

The function $`h`$ is computable in polynomial time. More precisely, if $`x`$ is an integer multiple of $`10^n`$ we can compute the ideal $`I_x`$ exactly and an approximation of $`\stackrel{~}{x}\delta (I_x)`$ accurate to $`10^n`$ in time poly$`(\mathrm{log}D,\mathrm{log}x,n)`$.
Also $`h`$ is a periodic function with period $`R`$ and it is one-to-one within each period.

Proof Clearly from the definition, $`h`$ is periodic and one-to-one within each period.

Given $`x`$ we can compute the ideal $`I_x(a,b)`$ as follows. Recall that the action of $`\rho `$ and $`\rho ^1`$ on a reduced ideal can be computed in $`\mathrm{poly}\mathrm{log}D`$ time. We start with $`𝒪(\tau (D,2),1)`$ at $`\delta (𝒪)=0`$. Apply $`\rho `$ twice to get $`I_0=\rho ^2(𝒪)`$ with $`\mathrm{ln}D>\delta (I_0)>\mathrm{ln}2`$. Write $`A=\delta (I_0)`$. Compute a sequence of ideals $`I_0,I_1,I_2,\mathrm{}`$ by repeated $``$squaring i.e. $`I_{k+1}=I_kI_k`$. Hence

$$2\delta (I_{k1})\delta (I_k)2\delta (I_{k1})+\frac{1}{2}\mathrm{ln}D$$

and so $`\delta (I_k)A2^k`$ (and we are not reducing the distance $`\mathrm{mod}R`$). We terminate the iteration with $`k=N`$ when $`\delta (I_{N+1})`$ first exceeds $`x`$, which always happens for $`N<\mathrm{log}_2(x/A)`$.

Now take $`I_N`$ and continue moving towards $`x`$ from the left in successively smaller steps, first using $``$products with $`I_{N1}`$, then $`I_{N2}`$ etc, ensuring each time to stay to the left of $`x`$. More precisely if we are at a reduced ideal $`J`$ and we are using $`I_k`$ then we compute $`JI_k`$ with $`\delta (J)+\delta (I_k)\delta (JI_k)\delta (J)+\delta (I_k)+\frac{1}{2}\mathrm{ln}D`$ and replace $`J`$ by $`JI_k`$ so long as $`\delta (JI_k)<x`$. If $`\delta (JI_k)>x`$ we discard it and try $`JI_{k1}`$. Finally after $`O(N)=O(\mathrm{log}x)`$ steps, finishing with $`I_1`$ we will have computed a reduced ideal $`J_{}`$ to the left of $`x`$ with $`x\delta (J_{})A+\frac{1}{2}\mathrm{ln}D\frac{3}{2}\mathrm{ln}D`$.

While doing all this with the ideals we also carry along a parallel computation of their accumulating distances (but not mod $`R`$) using the formula $`\delta (I,\rho (I))=\mathrm{ln}|\gamma |`$ for $`I=+\gamma `$ and proposition 35, calculating to a sufficient accuracy that will give the final $`\delta (J)`$ (at the end of our process continuing below) to the desired accuracy.

Finally we repeatedly apply $`\rho ^2`$ to $`J_{}`$ until the distance again exceeds $`x`$. This will require at most $`(\frac{3}{2}\mathrm{ln}D)/\mathrm{ln}2`$ steps. If $`J^{}`$ is the last such ideal to the left of $`x`$ then $`I_x`$ will certainly be either $`J^{}`$ or $`\rho (J^{})`$ which we can determine by finally checking if $`\delta (\rho (J^{}))`$ exceeds $`x`$ or not.

As mentioned above, while doing all this with the ideals we also carry along a parallel computation of their accumulating distances. Since $`n`$ digit arithmetical operations are computable with poly$`(n)`$ effort, this whole process gives the full value of $`h(x)`$ – with $`I_x`$ precisely and $`\stackrel{~}{x}\delta (I_x)`$ to $`n`$ digits of accuracy – with poly$`(\mathrm{log}D,\mathrm{log}x,n)`$ effort. By proposition 31, if $`10^n<d_{\mathrm{min}}=3/(32D)`$ (a lower bound on the minimum distance between ideals), we will certainly have located the nearest form to the left of $`x`$. (If $`n`$ is not sufficiently large, we still always work to an accuracy which is at least as fine as the above bound.)

Proof of proposition 2 Given $`R`$ or $`R`$ we use theorem 5 to compute the closest ideal $`I`$ to the left of $`R`$ and also its distance (to any desired accuracy). Thus $`\delta (\rho (I))>R`$ and $`\delta (\rho ^3(I))>R+\mathrm{ln}2>R`$. It follows that either $`\rho (I)`$ or $`\rho ^2(I)`$ must be $`𝒪=J_0`$ and its distance (that we compute) gives $`R`$.

## 10 The quantum algorithm for irrational periods on $``$

Suppose we have a function on $``$ that is periodic with (possibly irrational) period $`R`$

$$f:Xf(x+R)=f(x)\text{for all }x\text{.}$$

To apply the quantum period finding algorithm we will need to suitably discretise $`f`$, by taking values that are integer multiples $`k/N`$ of $`1/N`$ (for suitably large $`N`$) and if $`X`$ also contains continuous variables it should be discretised too, to ensure that exact calculations can be performed.

For example if $`f:`$ then we could define

$$\stackrel{~}{f}:\frac{1}{N}$$

by

$$\stackrel{~}{f}(k)=f(\frac{k}{N})_N$$

where we use the notation $`x_N`$ to denote the value of $`x`$ rounded down to the nearest multiple of $`1/N`$ (and similarly $`x_N`$ for rounding up). We would want $`\stackrel{~}{f}`$ to contain suitable approximate information about the period $`R`$ but unfortunately this is not guaranteed: suppose that $`f`$ has a very large variation in the region of diameter $`1/N`$ around $`x=k/N`$. Then although $`f(k/N)=f(k/N+lR)`$ exactly for all $`l`$, if we round $`lR`$ down (or up) to the nearest multiple of $`1/N`$ then the values of $`f(k/N+lR_N)`$ could vary arbitrarily with $`l`$ because (for irrational $`R`$) the rounding gap $`0lRlR_N1/N`$ is generally dense in the interval $`[0,1/N]`$ as $`l`$ ranges over $``$. Thus the periodicity may not be evident (even approximately) in $`\stackrel{~}{f}`$. To rule out such behaviour, we introduce the following notion of “weak periodicity” which will suffice for our applications.

Definition A function $`f:X`$ is called weakly periodic with period $`S`$ if for each $`0kS`$ and each $`l`$

$$\text{either }f(k+lS)\text{ or }f(k+lS)\text{ equals }f(k).$$
(14)
We write $`f(k)=f(k+[lS])`$ where the notation $`[lS]`$ denotes a chosen one of the two values $`lS`$ or $`lS`$ (and the choice may vary with $`k`$ and $`l`$).

Remark In our applications $`f`$ with period $`S`$ will arise from a computational problem with some input size $`\sigma `$ and $`S`$ will grow as $`\mathrm{log}S=O(\mathrm{poly}(\sigma ))`$. Then we will require the condition eq. (14) to hold only for a suitably large fraction $`1\frac{1}{\mathrm{poly}(\sigma )}`$ of the values $`0kS`$.

Recall our fundamental function with period $`R`$ (the regulator):

$$h:𝒫_{\mathrm{red}}\times h(x)=(I_x,\stackrel{~}{x}\delta (I_x))$$

where $`I_x`$ is the reduced principal ideal closest in distance to the left of $`x`$ and $`\stackrel{~}{x}\delta (I_x))`$ is the distance gap from $`I_x`$ to $`x`$. Define

$$\stackrel{~}{h}_N:𝒫_{\mathrm{red}}\times \frac{1}{N}\stackrel{~}{h}_N(k)=(I_{k/N},k/N\delta (I_{k/N})_N)$$

i.e. we compute $`h(x)`$ for $`x=k/N`$ and round the distance gap value down to the nearest integer multiple of $`1/N`$.

###### Proposition 36

(i) $`\stackrel{~}{h}_N`$ is one-to-one on $`0kNR`$.
(ii) $`\stackrel{~}{h}_N(k)`$ is computable in $`\mathrm{poly}(\mathrm{log}k,\mathrm{log}N,\mathrm{log}d)`$ time, so if $`N`$ and $`k`$ are $`O(\mathrm{poly}(d))`$ then $`\stackrel{~}{h}_N(k)`$ is computable in time $`\mathrm{poly}(\mathrm{log}d)`$.
(iii) Let $`d_{min}=3/(32D)`$ be a lower bound on the minimum distance between reduced ideals, and recall that $`\sigma =\mathrm{log}d`$ is the input size for the computation. If $`N`$ is sufficiently large i.e. $`1/N<d_{min}/\mathrm{log}d`$ then $`\stackrel{~}{h}_N`$ is weakly periodic with period $`NR`$. In fact the condition eq. (14) holds at all values $`0kNR`$ except possibly at the largest multiple $`k/N`$ of $`1/N`$ to the left of each reduced ideal (which is at most a fraction $`1/\mathrm{log}d`$ of the values).

Proof (i) $`\stackrel{~}{h}_N(k)=\stackrel{~}{h}_N(l)`$ means that $`k/N`$ and $`l/N`$ have the same nearest ideal on the left and the distances to it (from $`k/N,l/N`$) are the same when rounded down to the nearest multiple of $`1/N`$. But $`|k/Nl/N|1/N`$ if $`kl`$ so the rounded distances must then be different. Hence $`\stackrel{~}{h}_N(k)=\stackrel{~}{h}_N(l)k=l`$.
(ii) This is an immediate consequence of theorem 5 noting also that arithmetic operations with integers of size $`O(N)`$ (such as rounding operations) can be performed in $`\mathrm{poly}(\mathrm{log}N)`$ time.
(iii) Consider any fixed value $`0kNR`$. Let $`I^{}`$ at distance $`\delta (I^{})=x_0k/N`$ be the nearest ideal to the left of $`k/N`$, with distance gap $`n_0/N+ϵ_1`$ where $`n_0`$ and $`0<ϵ_1<1/N`$. Since $`h`$ is exactly periodic, $`I^{}`$ is also the nearest ideal to the left of $`k/N+lR`$, having the same distance gap. But now $`k/N+lR`$ is not an exact multiple of $`1/N`$ so consider rounding it up and down to $`k/N+lR_N`$ and $`k/N+lR_N`$. Let the corresponding rounding distances be $`ϵ_2`$ (down) and $`ϵ_3`$ (up). Thus $`ϵ_2+ϵ_3=1/N`$. We then have the following unrounded distances back to $`I^{}`$:
from $`k/N`$: $`n_0/N+ϵ_1`$;
from $`k/N+lR_N`$: $`n_0/N+ϵ_1ϵ_2=n_0/N+ϵ_11/N+ϵ_3`$;
from $`k/N+lR_N`$: $`n_0/N+ϵ_1+ϵ_3`$.
Hence if $`ϵ_1+ϵ_3>1/N`$ then $`k/N`$ and $`k/N+lR_N`$ will have the same rounded distances and $`\stackrel{~}{h}_N(k)=\stackrel{~}{h}_N(k+lR)`$. (Note that the rounded down position $`k/N+lR_N`$ can never pass to the left of $`I^{}`$ because $`ϵ_1+ϵ_3>1/N`$ so the unrounded distance $`n_0/N+ϵ_11/N+ϵ_3>n_0/N`$).
If $`ϵ_1+ϵ_31/N`$ then $`k/N`$ and $`k/N+lR_N`$ will have the same rounded distances to $`I^{}`$. But rounding $`k/N+lR`$ up to $`k/N+lR_N`$ may cross over another ideal $`I^{\prime \prime }`$ located in the gap between these two values. This can happen only if $`k/N`$ is the greatest multiple of $`1/N`$ to the left of $`I^{\prime \prime }`$. Thus, except for this eventuality, we have $`\stackrel{~}{h}_N(k)=\stackrel{~}{h}_N(k+lR)`$.
In summary, the weak periodicity condition eq. (14) must hold at all $`k`$ except possibly those $`k`$ with $`k/N`$ being the largest such value to the left of a reduced ideal. Now if $`1/N<d_{min}/\mathrm{log}d`$ i.e. $`N>\mathrm{log}d/d_{min}`$ then we have at least $`\mathrm{log}d`$ points between any two ideals so the weak periodicity condition will hold at least for a fraction $`(\mathrm{log}d1)/\mathrm{log}d=11/\mathrm{log}d`$ i.e. $`11/\mathrm{poly}(\mathrm{log}d)`$, of all $`k`$ values.

###### Theorem 6

Suppose that $`f:X`$ is weakly periodic with period $`S`$ and
(a) $`f(k)`$ is computable in $`\mathrm{poly}(\mathrm{log}k,\mathrm{log}S)`$ time;
(b) $`f`$ is one-to-one for $`0kS`$;
(c) given an integer $`m`$ there is a $`\mathrm{poly}(\mathrm{log}S)`$ time algorithm that will test if $`m`$ is close to an integer multiple of $`S`$ or not i.e. if $`|jSm|<1`$ for some $`j`$ or not.
Then there is a quantum algorithm with running time $`\mathrm{poly}(\mathrm{log}S)`$ that outputs an integer $`a`$ with $`|Sa|<1`$ with probability $`1/\mathrm{poly}(\mathrm{log}S)`$.

Proof Introduce some further notation: $`x`$ for the nearest integer above or below $`x`$. Thus $`|xx|1/2`$.
We will use quantum Fourier sampling in a dimension $`q`$ with $`q3S^2`$ (cf. later for the origin of this choice) and $`q`$ a power of 2 (for ease of efficient implementation of the quantum Fourier transform). Construct the state

$$\frac{1}{\sqrt{q}}\underset{m=0}{\overset{q1}{}}|m|f(m)$$

which by (a) can be done in $`\mathrm{poly}(\mathrm{log}S)`$ time. Write

$$q=pS+rp,r0r<S$$

i.e. $`pS`$ is the largest multiple of $`S`$ that is $`q`$ so $`pSq`$. Measure the second register to obtain in the first register:

$$|\psi _0=\frac{1}{\sqrt{p}}\underset{i=0}{\overset{p}{}}|k+[iS]$$

for $`0kS`$ chosen uniformly.
(Note: here we have assumed that the weak periodicity condition eq. (14) holds for all $`k`$. If it holds only for a fraction $`11/\mathrm{poly}(\mathrm{log}S)`$ of the $`k`$ values, then the state $`|\psi _0`$ will be slightly modified and our estimates below will be altered by a suitably small ($`1/\mathrm{poly}(\mathrm{log}S)`$) amount. Our final conclusions will remain valid but for clarity we will omit explicit analysis of these extra obfuscating variations.)
Next apply the quantum Fourier transform $`\mathrm{mod}q`$ to $`|\psi _0`$ to obtain the state $`_ja_j|j`$. We will be interested in the output probabilities $`|a_j|^2`$ and these do not depend on $`k`$ (by the shift invariance property of the Fourier transform). Hence (wlog) we will set $`k=0`$. Then

$$a_j=\frac{1}{\sqrt{pq}}\underset{l=0}{\overset{p1}{}}e^{2\pi i\frac{j[lS]}{q}}.$$

Write $`[lS]=lS+\delta _l`$ with $`1<\delta _l<1`$. Consider those $`j`$’s that are nearest to an integer multiple of $`q/S`$:

$$j=\frac{kq}{S}=\frac{kq}{S}+ϵ0kS\text{ and }\frac{1}{2}ϵ\frac{1}{2}$$
(15)
and also those $`j`$’s that are not too large:

$$j<\frac{q}{\mathrm{log}S}.$$
(16)
For these $`j`$’s we have

$$\frac{j[lS]}{q}=(\frac{k}{S}+\frac{ϵ}{q})(lS+\delta _l)=\frac{ϵS}{q}+(\frac{k\delta _l}{S}+\frac{ϵ\delta _l}{q})\mathrm{mod}\mathrm{\hspace{0.17em}\hspace{0.17em}1}$$

where we have removed the integer $`\frac{k}{S}lS`$. Since we are taking $`j=kq/S+ϵ<q/\mathrm{log}S`$ and $`|ϵ|1/2`$ we get $`k/S<1/\mathrm{log}S+1/(2q)`$. Also $`|\delta _l|<1`$ and $`q>3S^2`$ so

$$|\frac{k\delta _l}{S}+\frac{ϵ\delta _l}{q}|<\frac{1}{\mathrm{log}S}+\frac{1}{2q}+\frac{1}{2q}\frac{2}{\mathrm{log}S}.$$

Then writing $`A=ϵSp/q`$ we have

$$a_j=\frac{1}{\sqrt{pq}}\underset{l=0}{\overset{p1}{}}e^{2\pi i(\frac{Al}{p}+\xi (l))}$$

where

$$\xi (l)=\frac{k\delta _l}{S}+\frac{ϵ\delta _l}{q}\text{has }|\xi (l)|\frac{2}{\mathrm{log}S}$$

and $`A=ϵSp/q`$ has (recalling $`pS<q`$)

$$|A|=|\frac{ϵSp}{q}||ϵ|\frac{1}{2}.$$

Hence by lemma 3 below there is a constant $`c`$ such that

$$|a_j|^2\frac{1}{pq}cp^2=\frac{cp}{q}\frac{cp}{pS}=\frac{c}{S}$$

i.e. for each $`j`$ satisfying eqs. (15) and (16) we have

$$prob(j)\frac{c}{S}$$

and they are uniformly distributed. How many such $`j`$’s are there? We have $`j=\frac{kq}{S}q/\mathrm{log}S`$ so $`0kS/\mathrm{log}S`$. Thus the probability of getting a $`j`$ value that satisfies eqs. (15) and (16) is $`c/\mathrm{log}S`$.

Running all the above twice we will obtain two such $`j`$ values (called $`c`$ and $`d`$):

$$c=\frac{kq}{S}d=\frac{lq}{S}$$
(17)
having $`\mathrm{gcd}(k,l)=1`$ with probability $`1/\mathrm{poly}(\mathrm{log}S)`$. (The $`\mathrm{gcd}(k,l)=1`$ condition is obtained with inverse polynomial probability by the prime number theorem). From $`c`$ and $`d`$ we want to extract the information of $`k`$. To do this we use properties of continued fractions: we show that $`k/l`$ is a convergent of the continued fraction of $`c/d`$, which then gives $`k`$ as a numerator of a convergent. We use the following basic property of continued fractions (cf. theorem 184 of ): If $`a,b`$ and $`|x\frac{a}{b}|\frac{1}{2b^2}`$ then $`a/b`$ is a convergent of the continued fraction of $`x`$. By lemma 2 below (which requires $`q>3S^2`$) we have

$$\left|\frac{c}{d}\frac{k}{l}\right|<\frac{1}{2l^2}$$

giving the required result.

Recall that $`c=kq/S`$. Then for each convergent $`c_n/d_n`$ of the continued fraction of $`c/d`$ we check if $`c_n=k`$ by computing $`m=c_nq/c`$ and using (c) (from the statement of the theorem) to check if it is within 1 of an integer multiple of $`S`$ or not. Then we output the smallest such $`m`$ that is within 1 of an integer multiple of $`S`$ (and with probability $`1/\mathrm{poly}(\mathrm{log}S)`$ this multiple is 1). The various rounding processes stay within the required accuracy because if $`c=kq/S`$ and $`q3S^2`$ then $`|Skq/c|1`$. (To see this write $`c=kq/S+ϵ`$ with $`|ϵ|1/2`$. Then

$$\frac{kq}{c}=\frac{S}{1+\frac{ϵS}{kq}}=\frac{S}{1+\alpha }$$

where $`|\alpha |<1/(6S)`$ for $`q3S^2`$. Thus $`kq/c=SS\alpha /(1+\alpha )`$ and $`|S\alpha /(1+\alpha )|<1/2`$.)

The result of the whole process above is to output an integer $`m`$ such that $`|Sm|<1`$ with probability $`1/\mathrm{poly}(\mathrm{log}S)`$.

###### Lemma 2

If $`q>3S^2`$ then

$$\left|\frac{c}{d}\frac{k}{l}\right|<\frac{1}{2l^2}$$

with $`k,l,c,d`$ as in eq. (17).

Proof Let $`c=kq/S+ϵ_k`$ and $`d=lq/S+ϵ_l`$ with $`|ϵ_k|`$ and $`|ϵ_l|`$ both $`1/2`$ and wlog take $`k<lS`$. Then

$$\left|\frac{c}{d}\frac{k}{l}\right|=\left|\frac{S(ϵ_klϵ_lk)}{l^2q+ϵ_lSl}\right|\left|\frac{S(l+k)}{2l^2q2Sl/2}\right|\frac{S}{lqS/2}$$

where in the second last inequality we have used the worst case $`ϵ_k=1/2`$ and $`ϵ_l=1/2`$. Finally note that

$$\frac{S}{lqS/2}\frac{1}{2l^2}$$

holds if $`q2lS+S/(2l)`$ so $`q3S^2`$ suffices (as $`lS`$).

###### Lemma 3

Let $`|A|1/2`$ and let $`\xi (l)`$ be any function satisfying $`|\xi (l)|<1/n`$ with $`nO(\mathrm{log}p)`$. Then there is a constant $`c`$ such that for all sufficiently large $`p`$:

$$X=\left|\underset{l=0}{\overset{p1}{}}e^{2\pi i(\frac{A}{p}l+\xi (l))}\right|^2cp^2.$$

Proof View $`b_l=\mathrm{exp}2\pi i(Al/p+\xi (l))`$ as points on the unit circle, being $`\xi (l)`$-perturbations of the evenly spaced points $`c_l=\mathrm{exp}2\pi iAl/p`$ for $`l=0,\mathrm{},p1`$ which range over a fraction $`|A|1/2`$ of the whole circle.

Introduce $`x,y`$ axes so that the $`c_l`$ points are mirror symmetric in the $`y`$ axis with the negative $`y`$ axis bisecting the unused part of the circle. For all sufficiently large $`p`$ it is clear that the total $`y`$ component of $`b_l`$, for any perturbation with $`\xi (l)<1/n`$, is positive (since most points will lie in the half circle having $`y0`$). We claim that the smallest value of $`X`$ (over all possible perturbations) will occur when $`\xi (l)=1/n`$ for all points $`c_l`$ having $`x<0`$ and $`\xi (l)=1/n`$ for all points having $`x>0`$ i.e. $`\xi (l)`$ rotates points away from the positive $`y`$ axis, down towards the negative $`y`$ axis, symmetrically on the two sides of the $`y`$ axis. This perturbation maintains zero total $`x`$ component (by symmetry) and hence has the least squared total $`x`$ component of all perturbations, and for each point we get the least possible positive, or most negative $`y`$ component amongst all perturbations. Hence the total $`y`$ component (always being positive) must attain its least possible value and so we have the least possible $`X`$ amongst all perturbations.

To obtain a lower bound on $`X`$ for this minimal perturbation note that since $`|A|1/2`$, negative $`y`$ values can occur (if at all) only in the arc of the circle of fraction $`1/n`$ below the $`\pm x`$ axes, which are counterbalanced by positive values in the corresponding arcs above the $`\pm x`$ axes. Hence all points in the arc $`(2\pi /n,\pi 2\pi /n)`$ will contribute uncancelled positive $`y`$ values so in particular

$$X[\text{number of points in arc }(\pi /4,3\pi /4)]^2[y\text{ component at angle }\pi /4)]^2.$$

Since $`|A|1/2`$ the number of points in $`(\pi /4,3\pi /4)`$ is at least $`p/3`$ (actually it’s only slightly less than $`p/2`$ for large $`n`$) and the $`y`$ component at $`\pi /4`$ is $`1/\sqrt{2}`$ so $`X(p^2/9)(1/2)=p^2/18`$ for all sufficiently large $`p`$.

Finally we can apply theorem 6 to $`\stackrel{~}{h}_N`$ of proposition 36 having $`S=NR`$ to get an integer $`m`$ with $`|NRm|<1`$ i.e. the value of $`R`$ to a tolerance of $`1/N`$. The validity of theorem 6’s requirement (c) in this case can be seen as follows.

Given an integer $`m`$ , to test if $`|jRm|<1`$ or not, for some $`j`$, we first compute $`I_m`$, the ideal closest to $`m`$ on the left (which can be done in $`\mathrm{poly}(\mathrm{log}m,\mathrm{log}d)`$ time (cf. theorem 5). Let $`I_m`$ be $`J_{i_0}`$ in the principal cycle. Now $`|jRm|<1`$ for some $`j`$ iff the ideal $`𝒪`$ (in the repeating periodic pattern of reduced ideals placed along $``$) is located at distance $`<1`$ from $`m`$. Recall also that $`\delta (J_i,J_{i+2})>\mathrm{ln}2`$ and $`2\mathrm{ln}2>1`$. Hence if we look at the ideals $`J_{i_04},J_{i_03},\mathrm{},J_{i_0+4}`$ (which can be efficiently constructed by applying $`\rho `$ and $`\rho ^1`$ to $`I_m`$, up to four times) we will be able to see if $`𝒪`$ lies within distance 1 of $`m`$.

Putting all this together we have proved:

###### Theorem 7

Given a square-free integer $`d`$ there is a quantum algorithm that will output the regulator $`R`$ of $`[\sqrt{d}]`$ to accuracy $`10^n`$ with running time $`\mathrm{poly}(\mathrm{log}d,n)`$ and success probability $`1/\mathrm{poly}(\mathrm{log}d,n)`$, so long as $`10^n`$ is sufficiently small: $`10^n<d_{min}/\mathrm{log}d`$.

In view of proposition 2, to get $`R`$ to accuracy $`10^n`$ it suffices to use the quantum algorithm in theorem 6 with a fixed (suitably large) value $`n_0`$ of $`n`$ so the success probability to get accuracy $`10^n`$ becomes $`1/\mathrm{poly}(\mathrm{log}d)`$ but (as expected) the running time remains $`\mathrm{poly}(\mathrm{log}d,n)`$.

## 11 Further Remarks

Hallgren’s algorithm computes the regulator of $`[\sqrt{d}]`$ in $`\mathrm{poly}\mathrm{log}d`$ time. The tenacious reader may wish to estimate the degree of the polynomial running time by assessing all the ingredients that we have described in detail.

It is interesting to compare the computational complexity of the task REG – computing the regulator of $`[\sqrt{d}]`$ – to that of the task FAC – factoring a given integer $`d`$ (especially as the latter also admits an efficient quantum algorithm viz. Shor’s algorithm). Write $`n=\mathrm{log}d`$. The best classical algorithms for FAC and REG are sub-exponential but super-polynomial, with running times $`\mathrm{exp}(O(n^{1/3}))`$ and $`\mathrm{exp}(O(n^{1/2}))`$ respectively. Thus REG is the harder task and indeed there is a known reduction of FAC to REG (i.e. an algorithm for REG can be used to achieve FAC with the same time complexity). However it is significant to note that the sub-exponential algorithms  (§8) for REG depend on the truth of a suitable generalised Riemann hypothesis (GRH) associated with zeta functions on the quadratic number field. Without this assumption the best algorithm for REG has exponential running time $`O(d^{1/4})`$ (in fact using the same mathematical formalism of reduced ideals that we presented). Furthermore FAC is in $`\mathrm{NP}\mathrm{co}\mathrm{NP}`$ whereas REG is known to be in NP only under the assumption that GRH is true.

Finally we mention that in addition to solving Pell’s equation, Hallgren’s algorithm may be readily adapted to give efficient solutions of two further fundamental problems of computational algebraic number theory (cf. ): the principal ideal problem and the computation of the so-called class group of $`[\sqrt{d}]`$ (and its size, the class number). We refer the reader to Hallgren’s paper  for a description of these further applications and here we only make a few brief remarks about the statement of these problems.

For the usual integers $``$ all ideals are principal ideals. However in the more general setting of the algebraic integers $`𝒪`$ in the quadratic number field $`[\sqrt{d}]`$ this is no longer true i.e. there exist ideals of $`𝒪`$ which are not of the form $`\alpha 𝒪`$. However proposition 11 remains true: any ideal has the form $`I=\alpha +\beta `$ (for suitable $`\alpha ,\beta [\sqrt{d}]`$ which are restricted by the requirement that $`I`$ be closed under multiplication by $`𝒪`$). The principal ideal problem is then: given an ideal as $`I=\alpha +\beta `$ determine whether it is a principal ideal (and if it is, compute a generator $`\gamma `$ such that $`I=\gamma 𝒪`$).

The class group of $`[\sqrt{d}]`$ provides a measure of how much the ideals of $`𝒪`$ can deviate from being principal. An ideal $`I`$ is called invertible if there exists an ideal $`J`$ such that $`IJ=𝒪`$ (using the product of ideals introduced in section 7.1). Clearly all principal ideals are invertible (as the inverse of $`\alpha 𝒪`$ is $`\frac{1}{\alpha }𝒪`$) and the set $`_{\mathrm{inv}}`$ of all invertible ideals is an abelian group under multiplication of ideals (with $`𝒪`$ being the identity). The subset $`𝒫`$ of all principal ideals is a subgroup and the class group $`𝒞`$ is defined to be the quotient $`𝒞=_{\mathrm{inv}}/𝒫`$. Now it can be shown that $`𝒞`$ is always a finite abelian group (cf. ) and the class group problem is to compute a set of generators of $`𝒞`$ and to compute the size of $`𝒞`$.

Much of the theory of reduced ideals that we developed for principal ideals can be readily extended to general ideals providing a tool for attacking these problems too. Furthermore there is a way of representing ideals in terms of binary quadratic forms on $``$. Roughly speaking, the ideal $`+\frac{b+\sqrt{D}}{2a}`$ is represented by the form $`ax^2+bxy+cy^2`$ where $`D=b^24ac`$ and $`x,y`$, and we can develop a corresponding theory of reduced forms. (See  for an exposition of the correspondence between ideals and quadratic forms). Gauss devoted much effort to the class group problem when formulated in these terms, before the introduction of the concept of ideals by E. Kummer in 1847.

Acknowledgements
RJ is supported by the UK Engineering and Physical Sciences Research Council. Much of this work was carried out during the Quantum Computation Semester (Fall 2002) at the Mathematical Sciences Research Institute, Berkeley California and the author is grateful for the hospitality of the MSRI. Finally, thanks to Sean Hallgren and Lisa Hales for helpful discussions and clarifications on the subject of these notes.