Primes Factors
Primes Factors
Number theory
x3 + y3 + z3 = 33
It is easy to find three real numbers x, y and z that satisfy the equation. For
example, we can choose
x = 3,
p3
y = 3,
p
3
z = 3.
However, nobody knows if there are any three integers x, y and z that would
satisfy the equation, but this is an open problem in number theory [6].
In this chapter, we will focus on basic concepts and algorithms in number
theory. Throughout the chapter, we will assume that all numbers are integers, if
not otherwise stated.
84 = 22 · 31 · 71 .
191
The number of factors of a number n is
k
Y
τ( n ) = (α i + 1),
i =1
because for each prime p i , there are α i + 1 ways to choose how many times it
appears in the factor. For example, the number of factors of the number 84 is
τ(84) = 3 · 2 · 2 = 12. The factors are 1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42 and 84.
The sum of factors of n is
a +1
k
Y α
Y k pi i −1
σ( n ) = (1 + p i + . . . + p i i ) = ,
i =1 i =1 pi − 1
where the latter formula is based on the geometric progression formula. For
example, the sum of factors of the number 84 is
23 − 1 32 − 1 72 − 1
σ(84) = · · = 7 · 4 · 8 = 224.
2−1 3−1 7−1
The product of factors of n is
µ( n) = nτ(n)/2 ,
because we can form τ( n)/2 pairs from the factors, each with product n. For
example, the factors of the number 84 produce the pairs 1 · 84, 2 · 42, 3 · 28, etc.,
and the product of the factors is µ(84) = 846 = 351298031616.
A number n is perfect if n = σ( n) − n, i.e., n equals the sum of its factors
between 1 and n − 1. For example, the number 28 is perfect, because 28 =
1 + 2 + 4 + 7 + 14.
Number of primes
It is easy to show that there is an infinite number of primes. If the number of
primes would be finite, we could construct a set P = { p 1 , p 2 , . . . , p n } that would
contain all the primes. For example, p 1 = 2, p 2 = 3, p 3 = 5, and so on. However,
using P , we could form a new prime
p1 p2 · · · p n + 1
that is larger than all elements in P . This is a contradiction, and the number of
primes has to be infinite.
Density of primes
The density of primes means how often there are primes among the numbers.
Let π( n) denote the number of primes between 1 and n. For example, π(10) = 4,
because there are 4 primes between 1 and 10: 2, 3, 5 and 7.
It is possible to show that
n
π( n ) ≈ ,
ln n
which means that primes are quite frequent. For example, the number of primes
between 1 and 106 is π(106 ) = 78498, and 106 / ln 106 ≈ 72382.
192
Conjectures
There are many conjectures involving primes. Most people think that the con-
jectures are true, but nobody has been able to prove them. For example, the
following conjectures are famous:
Basic algorithms
p
If a number n is not prime, it can be represented as a product a · b, where a ≤ n
p p
or b ≤ n, so it certainly has a factor between 2 and b nc. Using this observation,
we can both test if a number is prime and find the prime factorization of a number
p
in O ( n) time.
The following function prime checks if the given number n is prime. The
p
function attempts to divide n by all numbers between 2 and b nc, and if none of
them divides n, then n is prime.
bool prime(int n) {
if (n < 2) return false;
for (int x = 2; x*x <= n; x++) {
if (n%x == 0) return false;
}
return true;
}
The following function factors constructs a vector that contains the prime
factorization of n. The function divides n by its prime factors, and adds them
to the vector. The process ends when the remaining number n has no factors
p
between 2 and b nc. If n > 1, it is prime and the last factor.
vector<int> factors(int n) {
vector<int> f;
for (int x = 2; x*x <= n; x++) {
while (n%x == 0) {
f.push_back(x);
n /= x;
}
}
if (n > 1) f.push_back(n);
return f;
}
193
Note that each prime factor appears in the vector as many times as it divides
the number. For example, 24 = 23 · 3, so the result of the function is [2, 2, 2, 3].
Sieve of Eratosthenes
The sieve of Eratosthenes is a preprocessing algorithm that builds an array
using which we can efficiently check if a given number between 2 . . . n is prime
and, if it is not, find one prime factor of the number.
The algorithm builds an array a whose positions 2, 3, . . . , n are used. The value
a[ k] = 0 means that k is prime, and the value a[k] 6= 0 means that k is not a prime
and one of its prime factors is a[ k].
The algorithm iterates through the numbers 2 . . . n one by one. Always when a
new prime x is found, the algorithm records that the multiples of x (2 x, 3 x, 4 x, . . .)
are not primes, because the number x divides them.
For example, if n = 20, the array is as follows:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 0 2 0 3 0 2 3 5 0 3 0 7 5 2 0 3 0 5
The following code implements the sieve of Eratosthenes. The code assumes
that each element in a is initially zero.
The inner loop of the algorithm will be executed n/ x times for any x. Thus, an
upper bound for the running time of the algorithm is the harmonic sum
n
X
n/ x = n/2 + n/3 + n/4 + · · · + n/ n = O ( n log n).
x=2
In fact, the algorithm is even more efficient, because the inner loop will be
executed only if the number x is prime. It can be shown that the time complexity
of the algorithm is only O ( n log log n), a complexity very near to O ( n).
Euclid’s algorithm
The greatest common divisor of numbers a and b, gcd(a, b), is the greatest
number that divides both a and b, and the least common multiple of a and b,
lcm(a, b), is the smallest number that is divisible by both a and b. For example,
gcd(24, 36) = 12 and lcm(24, 36) = 72.
The greatest common divisor and the least common multiple are connected as
follows:
ab
lcm(a, b) =
gcd(a, b)
194
Euclid’s algorithm1 provides an efficient way to find the greatest common
divisor of two numbers. The algorithm is based on the following formula:
(
a b=0
gcd(a, b) =
gcd( b, a mod b) b 6= 0
For example,
The time complexity of Euclid’s algorithm is O (log n), where n = min(a, b). The
worst case for the algorithm is the case when a and b are consecutive Fibonacci
numbers. For example,
1
Euclid was a Greek mathematician who lived in about 300 BC. This is perhaps the first
known algorithm in history.
195
Modular exponentiation
There is often need to efficiently calculate the value of x n mod m. This can be
done in O (log n) time using the following recursion:
1
n=0
n n/2 n/2
x = x ·x n is even
x n−1 · x
n is odd
x m−1 mod m = 1
xϕ(m) mod m = 1
when x and m are coprime. Fermat’s theorem follows from Euler’s theorem,
because if m is a prime, then ϕ( m) = m − 1.
Modular inverse
The inverse of x modulo m is a number x−1 such that
xx−1 mod m = 1.
196
of 36/6 mod 17, we can use the formula 2 · 3 mod 17, because 36 mod 17 = 2 and
6−1 mod 17 = 3.
However, a modular inverse does not always exist. For example, if x = 2 and
m = 4, the equation
xx−1 mod m = 1
cannot be solved, because all multiples of the number 2 are even and the remain-
der can never be 1 when m = 4. It turns out that the value of x−1 mod m can be
calculated exactly when x and m are coprime.
If a modular inverse exists, it can be calculated using the formula
x−1 = xϕ(m)−1 .
x−1 = x m−2 .
Using this formula, we can calculate the modular inverse efficiently using the
modular exponentation algorithm.
The above formula can be derived using Euler’s theorem. First, the modular
inverse should satisfy the following equation:
xx−1 mod m = 1.
Computer arithmetic
In programming, unsigned integers are represented modulo 2k , where k is the
number of bits of the data type. A usual consequence of this is that a number
wraps around if it becomes too large.
For example, in C++, numbers of type unsigned int are represented mod-
ulo 232 . The following code declares an unsigned int variable whose value is
123456789. After this, the value will be multiplied by itself, and the result is
1234567892 mod 232 = 2537071545.
197
21.3 Solving equations
A Diophantine equation is an equation of the form
ax + b y = c,
where a, b and c are constants and we should find the values of x and y. Each
number in the equation has to be an integer. For example, one solution for the
equation 5 x + 2 y = 11 is x = 3 and y = −2.
We can efficiently solve a Diophantine equation by using Euclid’s algorithm.
It turns out that we can extend Euclid’s algorithm so that it will find numbers x
and y that satisfy the following equation:
ax + b y = gcd(a, b)
39 x + 15 y = 12
The equation can be solved, because gcd(39, 15) = 3 and 3 | 12. When Euclid’s
algorithm calculates the greatest common divisor of 39 and 15, it produces the
following sequence of function calls:
39 − 2 · 15 = 9
15 − 1 · 9 = 6
9−1·6 = 3
39 · 2 + 15 · (−5) = 3
39 · 8 + 15 · (−20) = 12,
198
Chinese remainder theorem
The Chinese remainder theorem solves a group of equations of the form
x = a 1 mod m 1
x = a 2 mod m 2
···
x = a n mod m n
m1 m2 · · · m n
Xk = .
mk
because
1
X k X k−
m k mod m k = 1.
Since all other terms in the sum are divisible by m k , they have no effect on the
remainder, and the remainder by m k for the whole sum is a k .
For example, a solution for
x = 3 mod 5
x = 4 mod 7
x = 2 mod 3
is
3 · 21 · 1 + 4 · 15 · 1 + 2 · 35 · 2 = 263.
Once we have found a solution x, we can create an infinite number of other
solutions, because all numbers of the form
x + m1 m2 · · · m n
are solutions.
199
Zeckendorf’s theorem
Zeckendorf’s theorem states that every positive integer has a unique repre-
sentation as a sum of Fibonacci numbers such that no two numbers are equal or
consecutive Fibonacci numbers. For example, the number 74 can be represented
as the sum 55 + 13 + 5 + 1.
Pythagorean triples
A Pythagorean triple is a triple (a, b, c) that satisfies the Pythagorean theorem
a2 + b2 = c2 , which means that there is a right triangle with side lengths a, b and
c. For example, (3, 4, 5) is a Pythagorean triple.
If (a, b, c) is a Pythagorean triple, all triples of the form ( ka, kb, kc) are also
Pythagorean triples where k > 1. A Pythagorean triple is primitive if a, b and
c are coprime, and all Pythagorean triples can be constructed from primitive
triples using a multiplier k.
Euclid’s formula can be used to produce all primitive Pythagorean triples.
Each such triple is of the form
( n2 − m2 , 2 nm, n2 + m2 ),
where 0 < m < n, n and m are coprime and at least one of n and m is even. For
example, when m = 1 and n = 2, the formula produces the smallest Pythagorean
triple
(22 − 12 , 2 · 2 · 1, 22 + 12 ) = (3, 4, 5).
Wilson’s theorem
Wilson’s theorem states that a number n is prime exactly when
( n − 1)! mod n = n − 1.
Hence, Wilson’s theorem can be used to find out whether a number is prime.
However, in practice, the theorem cannot be applied to large values of n, because
it is difficult to calculate the value of ( n − 1)! when n is large.
200