Generating Random Factored Gaussian Integers, Easily
Noah Lebowitz-Lockard Advisor: Carl Pomerance June 6, 2013
Abstract We introduce an algorithm to generate a random Gaussian integer with the uniform distribution among those with norm at most N, along with its prime factorization. Then, we show that the algorithm runs in polynomial time. The hard part of this algorithm is determining a norm at random with a specific distribution. After that, finding the actual Gaussian integer is easy. We also consider the analogous problem for Eisenstein integers and quadratic integer rings.
1 1 Generating Random Factored Numbers, Easily
Consider the following problem:
Given a positive integer N,generatearandomintegerlessthanorequaltoN with uniform distribution, along with its factorization in polynomial time. (In this context, poly- nomial time refers to a polynomial in the number of digits of N, not the size of N.So,the running time of our algorithm should be O(logk N), for some real k.)
At first glance, this seems very simple. Simply choose a random integer in the range [1,N] and factor it. However, there are no known polynomial time factorization algorithms. But, the problem does not explicitly state that we need to factor anything. We need a random factored number, not a method to factor random numbers. In 1988, Eric Bach presented an algorithm without any factorizations at all in his pa- per “How to Generate Random Factored Numbers” [2]. Bach’s algorithm requires O(log N) primality tests. In 2003, Adam Kalai presented another algorithm in his “Generating Ran- dom Factored Numbers, Easily”, which used O(log2 N)primalitytests[1].ThoughKalai’s algorithm is slower, it is also easier to understand and we shall spend the rest of the paper discussing modifications of it. Also note that though we will not factor any numbers, every one of the algorithms pre- sented in this paper will perform primality tests, which we can run in polynomial time. The two polynomial time primality algorithms are the Miller-Rabin Test and the Agrawal-Kayal- Saxena (AKS) Algorithm. A programmer running the algorithms in this paper should use ei- ther of the two primality tests just mentioned. Because Kalai’s algorithm requires O(log2 N) primality tests, using Miller-Rabin or AKS enables the algorithm to run in polynomial time. Either of these algorithms could be used as a subroutine within the program. Strictly speaking, the Miller-Rabin Test can only check compositeness. Running Miller- Rabin once will either tell you that a given number is composite or the test will be incon- clusive. If you run the test many times, and it is consistently inconclusive, then the given number is almost certainly prime. By running the test many times, we can be arbitrarily accurate, i.e. the probability of error is less than ✏ for a given positive real ✏. For a given function f,wedefineO˜(f)asO(f logk f), where k is some positive real number. Note that logk f grows much more slowly than f. Running the Miller-Rabin Test on N once has a running time of O˜(log2 N) assuming we perform multiplications using a Fast Fourier Transform [12]. If the Generalized Riemann Hypothesis is true, then the Miller- Rabin Test becomes deterministic after running it O(log2 N)times.Inthisscenario,we could determine whether or not a given integer is prime in O˜(log4 N)time[8]. AKS will tell you whether a given number is prime or composite with perfect accuracy, but it is significantly slower. AKS runs in O˜(log15/2 N) time [1]. Specifically, AKS runs in O((log N)15/2(2 + log log N)c)forsomeconstantc. H. W. Lenstra and Carl Pomerance recently found a faster deterministic algorithm that runs in time O((log N)6(2 + log log N)c) [9]. If perfect accuracy is required, then we suggest Lenstra and Pomerance’s algorithm. If you can settle for very good accuracy, then we suggest Miller-Rabin.
2 2 Kalai’s Algorithm
Here is the algorithm from Adam Kalai’s “Generating Random Factored Numbers, Easily” [7]. Algorithm 1. Given a positive integer N, this algorithm produces a random positive integer r N, along with its factorization, with uniform distribution. 1. Create a list of integers s s ... s =1, where s is chosen uniformly at 1 2 k 1 random in [1,N] and if si has been chosen and si > 1, then si+1 is chosen uniformly at random in [1,si]. Call this list S. The procedure terminates when 1 is chosen. When the procedure terminates, go to Step 2. 2. Let r be the product of the prime elements of S. 3. If r>N, return to Step 1. Otherwise, output r, along with its prime factorization, with probability r/N. If you did not output r, return to Step 1. For example, let N =100.Thealgorithmmightgeneratethelist[98, 41, 38, 3, 3, 1]. When we multiply the prime elements of the list, we obtain 369, so we would create a new list. We might obtain [70, 5, 5, 2, 1], in which case we would output 50 with probability 0.5. We have to prove two facts about this algorithm. 1. The algorithm generates each r N with probability 1/N . 2. The algorithm expects to use O(log2 N)primalitytests. How do we determine the probability of obtaining r? Consider the following “generalized prime factorization” of r: r = p↵p . p N Y (Throughout this paper, p will always be a prime.) For any p N, p↵p is the highest power of p that is a factor of r. Note that in our definition, ↵p =0ifp is not a factor of r.Inanormalprimefactorization,wewouldsimply ignore any prime that is not a factor of r, but in this case, we must include all primes less than or equal to N. In order for our algorithm to output r,thelistmustcontainexactly↵p copies of p for each p N.But,whatistheprobabilitythatthelistcontains↵p copies of p?Supposewe have not yet finished our list and every element of the list so far is greater than or equal to p.Then,itisstillpossibletoaddacopyofp to the list. The conditional probability of choosing p given that you are choosing some number in [1,p]is1/p.Theconditional probability of adding a number smaller than p is 1 (1/p). The probability that the list contains n copies of p is equal to the probability of adding one copy of pntimes in a row, then adding a number less than p,namely, 1 1 P (n copies of p)= 1 . pn p ✓ ◆ 3 Let P ⇤(r) be the probability that the product of the primes in a list is equal to r.Inother words, P ⇤(r)istheprobabilityofoutputtingr if we ran Kalai’s algorithm without Step 3. We can use the equation above to determine the initial probability that we obtain r: 1 1 1 1 P ⇤(r)= P (↵p copies of p)= 1 = 1 . p↵p p p↵p p p N p N ✓ ◆ p N p N ✓ ◆ Y Y Y Y Notice that the product of 1/p↵ for all p N is equal to 1/r. Also note that the product of 1 (1/p)isafunctionofN and not r.Wecandefine M as this product: N 1 M = 1 . N p p N ✓ ◆ Y We can express the product of obtaining r at the end of Step 2 more succinctly as
MN P ⇤(r)= . r Here, r is any positive integer supported on the primes in [1,N]. Step 3 states that if r N, then we should output r with probability r/N.Otherwise,weshouldnotoutputr at all. P (r)isequaltoP ⇤(r)timestheconditionalprobabilitythatthealgorithmoutputsr given that it is the product of the primes in the list. Here is the actual probability that we output r: r MN r MN P (r)=P ⇤(r) = = . · N r · N N Note that the probability that we output r does not actually depend on the value of r,as long as r N.Thus,Kalai’salgorithmoutputseverynumberlessthanorequaltoN with a uniform distribution. Now, we have to prove that Kalai’s algorithm requires an average of O(log2 N)primality tests. We will do this by showing that the algorithm produces an average of O(log N)lists and by showing that the average list has O(log N)distinctelements. We have just shown that the probability of the algorithm producing a list and outputting r is MN /N .ThereareN possible numbers that we can output. The probability that the algorithm terminates is equal to N(MN /N ), or MN .Therefore,theexpectednumberoflists 1 we have to produce is MN .InordertoestimateMN ,weintroduceMertens’TwoTheorems. Our proofs of them come from [10]. The value of comes from [6]. Definition. For any positive real number x, log p A(x)= . p p x X Theorem 1. For any real x 1, A(x)=logx + O(1).
4 If there were only finitely many primes, then the sum of (log p)/p for all prime p x would be bounded above by some constant C. But, log x diverges. Therefore, Theorem 1 provides an alternate proof for the infinitude of the primes.
Theorem 2. (Mertens’ First Theorem) As N approaches infinity, the sum of the reciprocals of the primes that are less than or equal to N becomes asymptotic to log log N.
Proof. In order to take the sum of 1/p for all p N,wewillsplititintotwoseparatesums: 1 log p 1 1 log p log p 1 1 = = + . p p log p log N p p log p log N p N p N ✓ ◆✓ ◆ p N p N ✓ ◆ X X X X Theorem 1 makes the first sum easy to handle:
1 log p A(N) 1 = =1+O . log N p log N log N p N ✓ ◆ X The second sum is more di cult. It appears as though we should be able to write it in terms of A(N), but it is di cult to see how. We can rewrite the argument by de-telescoping it, i.e. writing it as a sum of terms that contract:
N 1 1 1 1 1 = . log p log N log n log(n +1) n=p X ✓ ◆ Therefore,
N 1 log p 1 1 log p 1 1 = . p log p log N p log n log(n +1) p N ✓ ◆ p N n=p ✓ ◆ X X X We can switch the sums around. Instead, we are taking the sum of (1/ log n) (1 log(n+1)) for all p n and all n from 2 to N 1: N 1 log p 1 1 1 1 log p = p log p log N log n log(n +1) p p N ✓ ◆ n=2 ✓ ◆ p n X X X N 1 1 1 = A(n) . log n log(n +1) n=2 X ✓ ◆ Observe that 1/ log t,likealldi↵erentiablefunctions,istheintegralofitsownderivative:
N 1 N 1 1 1 n+1 1 A(n) = A(n) dt. log n log(n +1) 2 n=2 n=2 n t log t X ✓ ◆ X Z
5 For any t in the half-open interval [n, n +1), A(t)=A(n)becauseaprimemustbeall primes are integers. Every prime that is less than or equal to t is also less than or equal to n.Therefore,A(t)isaconstantovertheinterval[n, n + 1], allowing us to move A(n)inside the integral:
N 1 N 1 n+1 1 n+1 A(t) N A(t) A(n) 2 dt = 2 dt = 2 dt. n=2 n t log t n=2 n t log t 2 t log t X Z X Z Z Once again, we break our expression into a sum and put asymptotic estimates on both of its components: N A(t) N 1 N A(t) log t dt = dt + dt. t log2 t t log t t log2 t Z2 Z2 Z2 For the first integral, N 1 dt =loglogN log log 2. t log t Z2 Theorem 1 states that A(t)=logt + O(1). Therefore, A(t) log t is bounded above by some positive constant C. Hence, | |
N A(t) log t N 1 1 1 dt C dt = C = O(1). t log2 t t log2 t log 2 log N Z2 Z2 ✓ ◆ Putting all of this together gives us our result: 1 =loglogN + O(1). p p N X
Theorem 3. (Mertens’ Second Theorem) Let =0.5772 ... be the Euler-Mascheroni Con- stant. Then, lim MN log N = e . N !1 Proof. Let x be a real number with absolute value less than 1. The Taylor Expansion of log(1 + x)is x2 x3 1 ( x)k x + + ...= . 2 3 k Xk=1 Let x = 1/p for some prime p.Makingthissubstitutiongivesus 1 1 1 1 1 log 1 = + + ... = . p p 2p2 kpk ✓ ◆ ✓ ◆ Xk=1 We can also separate out the first term and put the other terms into a separate sum. Ul- timately, we will prove that for su ciently large primes p,onlythefirsttermreallya↵ects
6 our proof. This makes sense on an intuitive level. As p approaches infinity, 1/p2 becomes meaningless next to 1/p.Wehave
1 1 1 1 log 1 = . p p kpk ✓ ◆ Xk=2 At this point, we take the logarithm of MN in order to use this result.
1 1 1 1 1 log M =log 1 = log 1 = . N p p p kpk p N ✓ ◆ p N ✓ ◆ p N p N k=2 Y X X X X Mertens’ First Theorem states that the sum of 1/p for all p N is asymptotic to log log N. We will prove that the other sum in the equation above is on a smaller order and may be ignored. For any prime p,
1 1 1 1 1 1 1 1 1 < = = . kpk 2pk 2 pk 2p(p 1) p2 Xk=2 Xk=2 Xk=2 Therefore, 1 1 1 1 1 < < . kpk p2 n2 p N k=2 p N n=1 X X X X We can prove that the sum of the reciprocals of the squares converges. Note that the number m is equal to the integral of m over an interval of unit length. That allows us to put the integral into the last part of the equation:
1 1 1 1 1 1 1 n+1 1 =1+ =1+ =1+ dx. n2 (n +1)2 (n +1)2 (n +1)2 n=1 n=1 n=1 n=1 n X X X X Z Observe that for any real x in the interval [n, n +1],1/x2 > 1/(n +1)2.Therefore,the integral of 1/x2 on the range [n, n +1]isgreaterthantheintegralof1/(n +1)2.Wecanuse this fact to set an integral as the upper bound for our sum:
n+1 n+1 1 1 1 1 1 1 1 =1+ dx < 1+ dx =1+ dx =2. n2 (n +1)2 x2 x2 n=1 n=1 n n=1 n n=1 X X Z X Z Z (To be exact, the sum of the reciprocals of the squares is ⇡2/6, but that is beyond the scope of this paper.) To summarize, we have two di↵erent limits:
1 1 1 log log N, = O(1). p ⇠ kpk p N p N k=2 X X X Therefore, log M lim N = 1. N !1 log log N 7 We raise e to both sides in order to obtain an asymptotic limit for MN :
log log N 1 lim MN = e + O(1) = O(log N). N !1 In the equation above, C is some constant. Though we will not prove it here, C = e .Though 1 it is di cult to prove, one could verify the value of C by calculating MN and log N for very large values of N. From this point onward, whenever we refer to Mertens’ Theorem, we will be referring 1 to his second theorem. We expect to create MN lists. As N approaches infinity, the ratio between MN is asymptotic to e log N. Hence, MN = O(log N) and Kalai’s algorithm creates an average of O(log N)lists.But,howmanydistinctelementsdoesalistcontain?Letn be an integer that is less than or equal to N. The probability that the list contains at least one copy of n is 1/n.Ifthelistcontainsacopyofn,thenwemustcheckifn is prime exactly once. Otherwise, we do not have to check whether or not n is prime. The expected number of times the algorithm checks if n is prime when processing a given list is 1/n.Byadditivity of expectation, the expected number of primality tests required for a given list is
1 1 N 1 1+ + ...+ = . 2 N n n=1 X We can write both an upper and lower bound for this sum. Consider the integral of (1/x)dx as x goes from 1 to N.Wecansplitthisintegralintoasumofsmallerintegralsasfollows:
N 1 N 1 n+1 1 dx = dx. x x 1 n=1 n Z X Z The minimum value of 1/x on the interval [n, n +1]is1/(n +1).Thelengthoftheinterval [n, n + 1] is 1. Thus,
N 1 N 1 n+1 1 1 1 1 1 dx > = + + ...+ . x n +1 2 3 N n=1 n n=1 X Z X Adding 1 to both sides gives us a useful inequality:
1 1 N 1 1+ + ...+ < 1+ dx =1+logN. 2 N x Z1 The maximum value of 1/x on the interval [n, n +1]is1/n,sothat
N 1 N 1 N N 1 n+1 1 1 1 1 1 log N = dx = dx < < =1+ + ...+ . x x n n 2 N 1 n=1 n n=1 n=1 Z X Z X X
8 We can now write both an upper and lower bound for our sum. We have
N 1 log N< < 1+logN. n n=1 X Our takeaway is that N 1 = O(log N). n n=1 X Thus, the expected number of primality tests the algorithm performs for a given list is O(log N). Since we’ve seen that the expected number of lists is O(log N), Kalai’s algorithm does an average of O(log2 N)primalitytests.
3 The Gaussian Problem
Our new goal is to generate a random Gaussian integer with norm less than or equal to given integer N,alongwithitsfactorizationintoGaussianprimes.Fromhereon,N(z)willbethe norm of the complex number z.Ourplanofattackwillbeasfollows:
1. Find a formula for G(r), the number of Gaussian integers in the first quadrant with a given norm r.
2. Modify Kalai’s Algorithm so that the probability of outputting a given r is proportional to G(r).
3. For a given r, along with its factorization into rational primes, produce a random Gaussian integer with norm r,alongwithitsfactorizationintoGaussianprimes.
The Gaussian integers have four units, unlike the rational integers, which have 2. Through- out this section, we will only be concerned the outcome up to a unit. For simplicity, we may assume that the Gaussian primes we generate are all in the first quadrant. If not, we multiply them by a power of i and then they will be. However, this means that for a given output z, we cannot obtain iz, z,or iz.Torectifythis,thereadermayinsertaStep4attheendof the algorithm. Step 4 multiplies the Gaussian integer that the algorithm was just outputted by ik,wherek is a random integer in the range [0, 4]. Units will not seriously concern us in this section. In order to find a formula, we will introduce the function D and then prove that G is identical to D.WewilldothisbyshowingthatG and D both possess a certain set of properties, then proving that any two functions that possess all of these properties are identical. In order to define D,wemustdefineafewotherfunctionsfirst.
Definition. The divisor function d gives the number of positive divisors of a given integer.
9 Definition. Let r be a positive integer with the prime factorization used in the theorem above. Let r1 be the largest factor of r which only contains primes that are congruent to 1 mod 4. Let r3 be the largest factor of r which only contains primes that are congruent to 3 mod 4. Note that with this notation, there exists some nonnegative integer k such that k r =2 r1r3. Definition. Let r be a positive integer. Let D(r)bethefollowingfunction:
d(r ) r is a square, D(r)= 1 3 0otherwise. ⇢ At this point, we shall define a new property of functions and prove that d, D,andG all have this property.
Definition. Afunctionf : N R is multiplicative if f(mn)=f(m)f(n)forallrelatively prime m, n. !
e1 em Theorem 4. Let r be a positive integer with prime factorization p1 ...pm . Then,
d(r)=(e1 +1)...(em +1).
Proof. Let r0 be a factor of r. Any prime factor of r0 must also be a prime factor of r.The exponent of pi in r0 can be any number that is less than or equal to ei. In other words, f1 fm r0 = p1 ...pm ,whereeachfi is an integer in the range [0,ei]. There are ei +1possibilities for each fi.Therefore,thereare(e1 +1)...(em +1)factorsofr. Theorem 5. The function d is multiplicative.
e1 en f1 fm Proof. Let a = p1 ...pn and b = q1 ...qm be two relatively prime integers. Then, we can determine d(ab)=d(a)d(b). It is easy to write out d(a)d(b):
d(a)d(b)=(e1 +1)...(en +1)(f1 +1)...(fm +1).
Because a and b are relatively prime, there do not exist integers i and j such that pi = qj. This makes writing ab especially easy:
e1 en f1 fm ab = p1 ...pn q1 ...qm .
We then apply the divisor function to ab:
d(ab)=(e1 +1)...(en +1)(f1 +1)...(fm +1)=d(a)d(b).
Hence, d is multiplicative.
Theorem 6. The function D is multiplicative.
10 e1 en f1 fm Proof. Once again, let a = p1 ...pn and b = q1 ...qm .Supposea3 and b3 are both squares. Then, a3b3 is a square. hence, D(ab)=d(a1b1)=d(a1)d(b1)=D(a)D(b). Suppose either a3 or b3 is not a square. Then, a3b3 is not a square. Hence, (ab)3 is not a square. So, D(ab)=0=D(a)D(b). Thus, D is multiplicative. To prove that G is multiplicative, we need a few results. Definition. AGaussianprimeis a Gaussian integer z such that if z = ab,wherea and b are Gaussian integers, then either a or b is a unit. The fact that we can factor Gaussian integers comes from the following theorem: Theorem 7. (Fundamental Theorem of Gaussian Integers) [11] “The expression of an in- teger as a product of primes is unique, apart from the order of the primes, the presence of unities, and ambiguities between associated primes.” At this point, we will present an outline of the proof of the Fundamental Theorem. Definition. [5] Let R be a commutative ring. The subset I of R is an ideal if I is a group under addition and for any a I and r R, ar is an element of I. 2 2 Definition. An element a of R is irreducible if it is not a unit and its only divisors are 1 and itself up to a unit. It may seem as though we have just defined a prime, but in fact, for the Gaussian integers, the two concepts are one and the same. Definition. An element p R is prime if for any a, b R such that p ab,eitherp a or p b. 2 2 | | | Definition. AringR is a unique factorization domain if for every r R, there is a unique way of writing r as the product of irreducible elements, up to a change2 in units. Definition. AringR is a principal ideal domain if for every ideal I R, there exists some element r R such that I = rx x R . ✓ 2 { | 2 } Definition. AringR is a Euclidean domain if there exists a function : R N that satisfies the following two properties: ! 1. For any a, b R 0 , (ab) (a) (b). 2 \{ } 2. For any a, b R with b =0,thereexistq, r R such that (r) <