0% found this document useful (0 votes)
10 views

Primes Factors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Primes Factors

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 21

Number theory

Number theory is a branch of mathematics that studies integers. Number


theory is a fascinating field, because many questions involving integers are very
difficult to solve even if they seem simple at first glance.
As an example, let us consider the following equation:

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.

21.1 Primes and factors


A number a is a factor or divisor of a number b if a divides b. If a is a factor
of b, we write a | b, and otherwise we write a - b. For example, the factors of the
number 24 are 1, 2, 3, 4, 6, 8, 12 and 24.
A number n > 1 is a prime if its only positive factors are 1 and n. For example,
the numbers 7, 19 and 41 are primes. The number 35 is not a prime, because it
can be divided into the factors 5 · 7 = 35. For each number n > 1, there is a unique
prime factorization
α
n = pα1 1 pα2 2 · · · p k k ,

where p 1 , p 2 , . . . , p k are primes and α1 , α2 , . . . , αk are positive numbers. For exam-


ple, the prime factorization for the number 84 is

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:

• Goldbach’s conjecture: Each even integer n > 2 can be represented as a


sum n = a + b so that both a and b are primes.

• Twin prime conjecture: There is an infinite number of pairs of the form


{ p, p + 2}, where both p and p + 2 are primes.

• Legendre’s conjecture: There is always a prime between numbers n2


and ( n + 1)2 , where n is any positive integer.

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.

for (int x = 2; x <= n; x++) {


if (a[x]) continue;
for (int u = 2*x; u <= n; u += x) {
a[u] = x;
}
}

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,

gcd(24, 36) = gcd(36, 24) = gcd(24, 12) = gcd(12, 0) = 12.

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,

gcd(13, 8) = gcd(8, 5) = gcd(5, 3) = gcd(3, 2) = gcd(2, 1) = gcd(1, 0) = 1.

Euler’s totient function


Numbers a and b are coprime if gcd(a, b) = 1. Euler’s totient function ϕ( n)
gives the number of coprime numbers to n between 1 and n. For example,
ϕ(12) = 4, because 1, 5, 7 and 11 are coprime to 12.
The value of ϕ( n) can be calculated from the prime factorization of n using
the formula
k
α −1
Y
ϕ( n) = p i i ( p i − 1).
i =1

For example, ϕ(12) = 21 · (2 − 1) · 30 · (3 − 1) = 4. Note that ϕ( n) = n − 1 if n is prime.

21.2 Modular arithmetic


In modular arithmetic, the set of available numbers is limited so that only
numbers 0, 1, 2, . . . , m − 1 may be used, where m is a constant. Each number x is
represented by the number x mod m: the remainder after dividing x by m. For
example, if m = 17, then 75 is represented by 75 mod 17 = 7.
Often we can take the remainder before doing calculations. In particular, the
following formulas can be used:

( x + y) mod m = ( x mod m + y mod m) mod m


( x − y) mod m = ( x mod m − y mod m) mod m
( x · y) mod m = ( x mod m · y mod m) mod m
x n mod m = ( x mod m)n mod m

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

It is important that in the case of an even n, the value of x n/2 is calculated


only once. This guarantees that the time complexity of the algorithm is O (log n),
because n is always halved when it is even.
The following function calculates the value of x n mod m:

int modpow(int x, int n, int m) {


if (n == 0) return 1%m;
int u = modpow(x,n/2,m);
u = (u*u)%m;
if (n%2 == 1) u = (u*x)%m;
return u;
}

Fermat’s theorem and Euler’s theorem


Fermat’s theorem states that

x m−1 mod m = 1

when m is prime and x and m are coprime. This also yields

x k mod m = x k mod (m−1) mod m.

More generally, Euler’s theorem states that

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.

For example, if x = 6 and m = 17, then x−1 = 3, because 6 · 3 mod 17 = 1.


Using modular inverses, we can divide numbers modulo m, because division
by x corresponds to multiplication by x−1 . For example, to evaluate the value

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 .

If m is prime, the formula becomes

x−1 = x m−2 .

For example, if x = 6 and m = 17, then

x−1 = 617−2 mod 17 = 3.

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.

On the other hand, according to Euler’s theorem,

xϕ(m) mod m = xxϕ(m)−1 mod m = 1,

so the numbers x−1 and xϕ(m)−1 are equal.

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.

unsigned int x = 123456789;


cout << x*x << "\n"; // 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)

A Diophantine equation can be solved if c is divisible by gcd(a, b), and other-


wise the equation cannot be solved.

Extended Euclid’s algorithm


As an example, let us find numbers x and y that satisfy the following equation:

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:

gcd(39, 15) = gcd(15, 9) = gcd(9, 6) = gcd(6, 3) = gcd(3, 0) = 3

This corresponds to the following equations:

39 − 2 · 15 = 9
15 − 1 · 9 = 6
9−1·6 = 3

Using these equations, we can derive

39 · 2 + 15 · (−5) = 3

and by multiplying this by 4, the result is

39 · 8 + 15 · (−20) = 12,

so a solution to the equation is x = 8 and y = −20.


A solution to a Diophantine equation is not unique, but we can form an infinite
number of solutions if we know one solution. If a pair ( x, y) is a solution, then
also all pairs
kb ka
(x + , y− )
gcd(a, b) gcd(a, b)
are solutions, where k is any integer.

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

where all pairs of m 1 , m 2 , . . . , m n are coprime.


1
Let x−m be the inverse of x modulo m, and

m1 m2 · · · m n
Xk = .
mk

Using this notation, a solution to the equations is


1 −1 −1
x = a1 X 1 X 1 −
m1 + a 2 X 2 X 2 m2 + · · · + a n X n X n m n .

In this solution, it holds for each number k = 1, 2, . . . , n that


1
ak X k X k−
m k mod m k = a k ,

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.

21.4 Other results


Lagrange’s theorem
Lagrange’s theorem states that every positive integer can be represented as a
sum of four squares, i.e., a2 + b2 + c2 + d 2 . For example, the number 123 can be
represented as the sum 82 + 52 + 52 + 32 .

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.

For example, the number 11 is prime, because

10! mod 11 = 10,

and the number 12 is not prime, because

11! mod 12 = 0 6= 11.

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

You might also like