0
Product of Primes based Integer Decomposition Roland Bruggmann
[email protected] Version 0.6 (working copy) 27th July 2017
Abstract Integer factorisation, primality testing and prime number generation—the solving of three problems in number theory by the use of a deterministic algorithm for integer decomposition based on the product of primes. The hereby presented algorithms time complexities are in O(n). Index Terms—Number Theory, Product of Primes, Integer Factorisation, Primality Testing, Prime Number Generation.
Versions: 2015-10-02 2015-10-06 2015-12-16 2016-01-11 2016-05-23 2016-07-05
Version Version Version Version Version Version
0.1 0.2 0.2 0.3 0.4 0.5
Working Working Review Working Working Working
Copy Copy Copy Copy Copy
Contents 1 The Exponents in the Product of Primes 2 Numeric Methods 2.1 Numberal Systems of Base 2.2 Factorisation . . . . . . . 2.3 Primality Testing . . . . . 2.4 Prime Number Generation
of Primes . . . . . . . . . . . . . . . . . .
. . . .
. . . .
. . . .
2
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
3 Algorithms
6 6 6 7 7 8
List of Algorithms 3.1 3.2 3.3 3.4
Integer decomposition . . Factorisation . . . . . . . Primality testing . . . . . Prime number generation
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
8 8 9 9
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
3 4 4 4
List of Figures 1 2 3 4
2-adic 3-adic 5-adic 7-adic
valuation valuation valuation valuation
of of of of
n, n, n, n,
for for for for
0 ≤ n ≤ 200 0 ≤ n ≤ 200 0 ≤ n ≤ 200 0 ≤ n ≤ 140
1
1
The Exponents in the Product of Primes
Riesel [Rie94, p. 2] notes that the fundamental theorem of arithmetic “well illustrates the fundamental role of the prime numbers”. Every positive integer n ∈ N can be written as a product of primes p, with s as the number of primes ≤ n and in one way only (ibid.): αs α2 1 n = pα 1 × p2 × · · · × ps =
s Y
i pα i
(1)
i=1
Let us focus on the exponents αi of the primefactors pi . First, we start an iteration over a positive integer n ∈ N, e.g., from 2 to 28 and build the products of primes with pi ≤ n: 2 = 21 3 = 20 × 31 4 = 22 × 30 5 = 20 × 30 × 51 6 = 21 × 31 × 50 7 = 20 × 30 × 50 × 71 8 = 23 × 30 × 50 × 70 9 = 20 × 32 × 50 × 70 10 = 21 × 30 × 51 × 70 11 = 20 × 30 × 50 × 70 × 111 12 = 22 × 31 × 50 × 70 × 110 13 = 20 × 30 × 50 × 70 × 110 × 131 14 = 21 × 30 × 50 × 71 × 110 × 130 15 = 20 × 31 × 51 × 70 × 110 × 130 16 = 24 × 30 × 50 × 70 × 110 × 130 17 = 20 × 30 × 50 × 70 × 110 × 130 × 171 18 = 21 × 32 × 50 × 70 × 110 × 130 × 170 19 = 20 × 30 × 50 × 70 × 110 × 130 × 170 × 191 20 = 22 × 30 × 51 × 70 × 110 × 130 × 170 × 190 21 = 20 × 31 × 50 × 71 × 110 × 130 × 170 × 190 22 = 21 × 30 × 50 × 70 × 111 × 130 × 170 × 190 23 = 20 × 30 × 50 × 70 × 110 × 130 × 170 × 190 × 231 24 = 23 × 31 × 50 × 70 × 110 × 130 × 170 × 190 × 230 25 = 20 × 30 × 52 × 70 × 110 × 130 × 170 × 190 × 230 26 = 21 × 30 × 50 × 70 × 110 × 131 × 170 × 190 × 230 27 = 20 × 33 × 50 × 70 × 110 × 130 × 170 × 190 × 230 28 = 22 × 30 × 50 × 71 × 110 × 130 × 170 × 190 × 230
We remarke that during the iteration, the exponent of every primefactor builds a sequence, too. We like to name these sequences — say, we call them α-sequences. Definition 1. For a squence of positive numbers n ∈ N, we build the products of primes. The exponent αi of a primefactor pi builds a sequence, too. The sequence is called α-sequence Sα , written as Sαi .
2
Let’s have a look at these α-sequences for the primefactors pi with 1 ≤ i ≤ 4:
For primefactor p1 = 2, the exponent α1 builds the sequence Sα1 = (0), 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2 (. . . ) For primefactor p2 = 3, the exponent α2 builds the sequence Sα2 = (0, 0), 1, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 1, 0, 0, 3, 0 (. . . ) For primefactor p3 = 5, the exponent α3 builds the sequence Sα3 = (0, 0, 0, 0), 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0 (. . . ) For primefactor p4 = 7, the exponent α4 builds the sequence Sα4 = (0, 0, 0, 0, 0, 0), 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 (. . . ) Such an α-sequence Sα can be seen as a sequence of p-adic valuations of n. The j th element aj is of type aj = vpi (nk ). For i = 1, p1 is 2 and Sα1 is a0 = v2 (n1 ) = v2 (1) = 0 a1 = v2 (n2 ) = v2 (2) = 1 a2 = v2 (n3 ) = v2 (3) = 0 a3 = v2 (n4 ) = v2 (4) = 2 a4 = v2 (n5 ) = v2 (5) = 0 a5 = v2 (n6 ) = v2 (6) = 1 If we start the iteration with n = 1, the counter k has the same value as n — therefore, we can write an = vp (n). Theorem 1. An α-sequence Sα is a sequence of p-adic valuations of n. It consists of elements an = vp (n) for 1 ≤ n ≤ ∞. Several valuations of n can be found in the On-Line Encyclopedia of Integer Sequences [OEIS]:
The α-sequence of exponent α1 is the p1 -adic, therefore 2-adic valuation of n: Sα1 = v2 (n) for 1 ≤ n ≤ ∞, as in [OEIS, sequence A007814] (see figure 1). The α-sequence of exponent α2 is the p2 -adic, therefore 3-adic valuation of n: Sα2 = v3 (n) for 1 ≤ n ≤ ∞, as in [OEIS, sequence A007949] (see figure 2). The α-sequence of exponent α3 is the p3 -adic, therefore 5-adic valuation of n: Sα3 = v5 (n) for 1 ≤ n ≤ ∞, as in [OEIS, sequence A112765] (see figure 3). The α-sequence of exponent α4 is the p4 -adic, therefore 7-adic valuation of n: Sα4 = v7 (n) for 1 ≤ n ≤ ∞, as in [OEIS, sequence A214411] (see figure 4).
Figure 1: 2-adic valuation of n, for 0 ≤ n ≤ 200
3
Figure 2: 3-adic valuation of n, for 0 ≤ n ≤ 200
Figure 3: 5-adic valuation of n, for 0 ≤ n ≤ 200
Figure 4: 7-adic valuation of n, for 0 ≤ n ≤ 140
4
In general, the product of primes can be written as vp1 (n)
n = p1
vp2 (n)
× p2
v
× · · · × psps
(n)
=
s Y
vpi (n)
pi
(2)
i=1
or as prime power decomposition: n=
Y
pvp (n)
(3)
p
Theorem 2. Every positive integer n ∈ N can be written as a product of primes p with p-adic valuations of n as exponents, and with s as the number of primes p ≤ n.
5
2
Numeric Methods
To achieve the solving of prime based problems using the product of primes in equation 2, we simulate the valuation by the use of numberal systems of base of primes.
2.1
Numberal Systems of Base of Primes
For the replacement of the valuation, we make use of a function which counts the trailing zeros of a number (see definition 2). Definition 2. For a value x ∈ N, the function count trailing zeros ctz (x) counts the trailing zeros of x. First, we set n to numberal systems which all are of base of primes npi , with i = 1 . . . s. Let’s have a look at a short sequence of positive integers, e.g., 18 and 19 in different numberal systems of base of prime, by means of n10 = np1 = np2 = · · · = nps : 1810 = 100102 = 2003 = 335 = 247 = 1711 = 1513 = 1117 1910 = 100112 = 2013 = 345 = 257 = 1811 = 1613 = 1217 = 1019 Now we get the exponents αi by counting the trailing zeros of n in base of pi , or formaly spoken: αi = ctz (npi ), f or i = 1 . . . s
(4)
For n = 18 this is: α1 = ctz (100102 ) = 1 α2 = ctz (2003 ) = 2 α3 = ctz (335 ) = 0 α4 = ctz (247 ) = 0 α5 = ctz (1711 ) = 0 α6 = ctz (1513 ) = 0 α7 = ctz (1117 ) = 0 For n = 19 this is: α1 = ctz (100112 ) = 0 α2 = ctz (2013 ) = 0 α3 = ctz (345 ) = 0 α4 = ctz (257 ) = 0 α5 = ctz (1811 ) = 0 α6 = ctz (1613 ) = 0 α7 = ctz (1217 ) = 0 α8 = ctz (1019 ) = 1
Theorem 3. In the product of primes p, an exponent α is given by αi = ctz (npi ), for i = 1 . . . s.
2.2
Factorisation
We rewrite equation 2 by the use of function ctz — and that’s a factorisation method, too (see equation 5). ctz (np1 )
n = p1
ctz (np2 )
× p2
ctz (nps )
× · · · × ps
=
s Y
ctz (npi )
pi
(5)
i=1
Theorem 4. Every positive integer n ∈ N can be written as a product of primes p with counted trailing zeros of n in base of pi as exponent αi , and with s as the number of primes ≤ n.
6
2.3
Primality Testing
If a positive integer n ∈ N is prime, then n = ps , with s as the number of primes p ≤ n. The product of primes then shows exponents α1 . . . αs−1 with values of 0, and αs is 1. In our case, we replace αs with ctz (nps ) (see equation 6). n ∈ P ⇒ ctz (nps ) = 1 (6)
Theorem 5. If a positive integer n ∈ N is prime, then ctz (nps ) = 1, with s as the number of primes p ≤ n. Perhaps, we like to know if n is prime without comparing n with ps . Then we can count the trailing zeros of n in base of ps — if ctz (nps ) is 1, then n is prime (see equation 6). ctz (nps ) = 1 ⇒ n ∈ P
(7)
Theorem 6. A positive integer n ∈ N is prime, if ctz (nps ) = 1, with s as the number of primes p ≤ n.
2.4
Prime Number Generation
The generation of primes can be seen as based on a function which checks a number for having a trailing zero (see definition 3). Definition 3. For a value x ∈ N, the function trailing zero tz (x) returns 1 if x has at least one trailing zero. Otherwise, if x has no trailing zero, the function returns 0. We use the function tz (x) for counting the number of exponents αi > 0, as we build the sum t, with s as the number of primes p ≤ n (see equation 8). s X t= tz (npi ) (8) i=1
We start the prime number generation with a positive integer n > 1, and with s as the number of primes ≤ n listed in Ps = {p1 , . . . , ps }. Then we increment integer n to n + 1. If the sum t is 0, then n is a new primefactor — we increment the primecounter s and ps is n. 1. n ∈ N and n > 1; s: number of primes ≤ n; Ps = {p1 , p2 , . . . , ps } 2. increment n P 3. if t = si=1 tz (npi ) = 0, then increment s and ps = n Let’s have a look at a short sequence of positive integers, e.g., we start with n = 18: 1. n = 18; s = 7; P7 = {2, 3, 5, 7, 11, 13, 17} 2. Increment n; n = 19 P 3. t = 7i=1 tz (19pi ) = tz (100112 ) + tz (2013 ) + tz (345 ) + tz (257 ) + tz (1811 ) + tz (1613 ) + tz (1217 ) = 0 ⇒ increment s; s = 8 and ps = p8 = 19 1. n = 19; s = 8; P8 = {2, 3, 5, 7, 11, 13, 17, 19} 2. Increment n; n = 20 P 3. t = 8i=1 tz (20pi ) = tz (101002 ) + tz (2023 ) + tz (405 ) + tz (267 ) + tz (1911 ) + tz (1713 ) + tz (1317 ) + tz (1119 ) = 2 6= 0 1. n = 20; s = 8; P8 = {2, 3, 5, 7, 11, 13, 17, 19} 2. Increment n; n = 21 P 3. t = 8i=1 tz (21pi ) = tz (101012 ) + tz (2103 ) + tz (415 ) + tz (307 ) + tz (1A11 ) + tz (1813 ) + tz (1417 ) + tz (1219 ) = 2 6= 0 And so on. Theorem 7. Given a positive integer n ∈ N and s as the number of primes p ≤ n. If, for the P next positive integer n + 1 as value in the different prime based number systems of n, the sum t of trailing zeros si=1 tz (npi ) is 0, then n + 1 is prime.
7
3
Algorithms
Based on the incremental prime number generation, a deterministic algorithm for integer decomposition can be performed as described in this section. The algorithm will be used for integer factorisation, primality testing, as well as for prime number generation:
The function decomposite performs a deterministic, incremental decomposition of an integer by the use of counters in different number systems of base of primes—exept of p1 = 2 as we start with the first odd prime number p2 = 3 and increment the counters by two—and by the use of a check of all counters for at least one trailing zero (tz) (see algorithm 3.1). The procedure factorise performs a factorisation of an integer. First, algorithm decomposite is called. Afterwards a counter with at least one trailing zero (tz) will be printed out as primefactor with counted trailing zeros (ctz) as exponent (see algorithm 3.2). The function isPrime performs a primality test. The result of algorithm decomposite will be proofed against two conditions (see algorithm 3.3): 1) A prime number has a single primefactor—by the means of itself (identity could be proofed in addition)—2) which must have an exponent of value one. The procedure generatePrimes generates prime numbers up to a given integer by calling algorithm decomposite first. Then the base of the counters will be printet out as they are all of different base of primes (see algorithm 3.4).
Algorithm 3.1 Integer decomposition Require: n ∈ N ∧ n > 1 1: function decomposite(n) 2: vectorOfCounters 3: trailingZero 4: i←3 5: vectorOfCounters.push back new counter of base i 6: vectorOfCounters.back .value ← 10i 7: while i < n do 8: { increment i twice } 9: { increment all counter .value in parallel twice } 10: trailingZero ← { parallel k of all counter .tz } 11: if ¬trailingZero then 12: vectorOfCounters.push back new counter of base i 13: vectorOfCounters.back .value ← 10i 14: end if 15: end while 16: return vectorOfCounters 17: end function
. # operations: .1 .1 .2 .2 .2 . (n − 1) · 3 . (n − 2) · 2 . Pn (n − 2) · 2 . (n − 2) · 2 + k=2 log2 π(k) . (n − 2) · 2 . (π(n) − 1) · 2 . (π(n) − 1) · 2
.2
Algorithm 3.2 Factorisation Require: n ∈ N ∧ n > 1 1: procedure factorise(n) 2: vectorOfCounters ← decomposite(n) 3: for all counter in vectorOfCounters do 4: if counter .tz then 5: print counter .base ’**’ counter .ctz newline 6: end if 7: end for 8: end procedure
8
. # operations: . 3 + T (decomposite(n)) . π(n) . π(n) . Ω(n) · 5
Algorithm 3.3 Primality testing Require: n ∈ N ∧ n > 1 Ensure: n is odd 1: function isPrime(n) 2: vectorOfCounters ← decomposite(n) 3: if vectorOfCounters.back .ctz == 1 then 4: return true 5: else 6: return false 7: end if 8: end function
. # operations:
. 3 + T (decomposite(n)) .2 . 2 or .2
Algorithm 3.4 Prime number generation Require: n ∈ N ∧ n > 1 1: procedure generatePrimes(n) 2: vectorOfCounters ← decomposite(n) 3: for all counter in vectorOfCounters do 4: print counter .base newline 5: end for 6: end procedure
. # operations: . 3 + T (decomposite(n)) . π(n) . π(n) · 3
Time complexity of algorithm 3.1: n X log π(k) + 10 log 2 k=2 2 3 n log log log log 2 log 3 log n 4n = 11n + + + + ··· + − 13 log n log 2 log 2 log 2
T (n) = 3 · (n − 1) + 8 · (n − 2) + 4 · (π(n) − 1) +
| π(n) '
n log n
⇒ T (n) ∈ O(n) As procedure factorise (algorithm 3.2), function isPrime (algorithm 3.3), and procedure generatePrimes (algorithm 3.4) all make use of function decomposite (algrithm 3.1), their time complexity is in O(n), too. We compare this value with the time complexities of the fastest known deterministic algorithms of nowadays:
According to Riesel [Rie94, p. 217], the fastest known deterministic factorisation method is the General 1/3 Number Field Sieve (GNFS) with a complexity in O(exp[ 64 log n (log log n)2/3 ]). 9 The fastest known deterministic primality tests are the Agrawal-Kayal-Saxena primality test (AKS) (cp. [AKS04]), improved by Pomerance and Lenstra [PL05] to a complexity in O(log6 n), and the deterministic variant of the Miller-Rabin primality test (MRT) with a complexity in O(log4 n). The so called primesieve (segmented sieve of Eratosthenes with wheel factorization) has a time complexity in O(n log log n), the complexity of the fastest known deterministic prime number generator, the Sieve of Atkin is in O(n).
References [AKS04]
Manindra Agrawal, Neeraj Kayal and Nitin Saxena. ‘PRIMES is in P’. In: (2004). doi: 10. 4007/annals.2004.160.781.
[OEIS]
N. J. A. Sloane. The On-Line Encyclopedia of Integer Sequences. url: http://oeis.org/ (visited on 01/2016).
[PL05]
Carl Pomerance and Jr. H. W. Lenstra. ‘Primality testing with Gaussian periods’. In: (2005). url: http://www.math.dartmouth.edu/~carlp/aks041411.pdf (visited on 13/09/2015).
[Rie94]
Hans Riesel. Prime Numbers and Computer Methods for Factorization. 2nd ed. Boston, MA: Birkh¨ auser, 1994. isbn: 0-8176-3743-5.
9