0% found this document useful (0 votes)
79 views5 pages

The Prime Numbers, Part 2

The document discusses methods for finding the prime factorization of integers. It begins by describing the naive method of repeatedly dividing the integer by its smallest non-trivial divisor. It then presents Python code to implement this algorithm. Next, it introduces Fermat's factorization method, which looks for two factors that are the difference of two squares. Python code for this method is also provided. Finally, it discusses approximations for π(x), the number of primes less than or equal to x, including the prime number theorem.

Uploaded by

Amiya Biswas
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)
79 views5 pages

The Prime Numbers, Part 2

The document discusses methods for finding the prime factorization of integers. It begins by describing the naive method of repeatedly dividing the integer by its smallest non-trivial divisor. It then presents Python code to implement this algorithm. Next, it introduces Fermat's factorization method, which looks for two factors that are the difference of two squares. Python code for this method is also provided. Finally, it discusses approximations for π(x), the number of primes less than or equal to x, including the prime number theorem.

Uploaded by

Amiya Biswas
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/ 5

The Prime Numbers, Part 2

The fundamental theorem of arithmetic says that every integer greater than
1 can be written as a product of primes, and furthermore there is only one
way to do it except for the ordering of the factors.

How can we actually find the prime factorization of a given integer n > 1?

The simplest and most naive approach goes as follows. Given n, find the
smallest non-trivial (i.e., not 1) divisor of n by testing (via the division algo-
rithm) the possible divisors in order from smallest to largest. The smallest
non-trivial divisor must be prime; call it p1 .

Now divide n by p1 to obtain a smaller integer n1 = n/p1 . If n/p1 = 1 then


stop; in this case n = p1 is prime. Otherwise, repeat the process on n/p1 .
Eventually you will obtain the prime factorization of n.
Example. Suppose n = 27489. We compute the prime factorization of n.
The smallest non-trivial divisor of n is 3, and 27489/3 = 9163. The smallest
non-trivial divisor of 9163 is 7, and 9163/7 = 1309. The smallest non-trivial
divisor of 1309 is again 7, and 1309/7 = 187. The smallest non-trivial divisor
of 187 is 11, and 187/11 = 17. Finally, the smallest non-trivial divisor of 17
is 17 itself, so 17 is prime, and we are finished:

n = 27489 = 3 · 7 · 7 · 11 · 17 = 3 · 72 · 11 · 17.

Here’s an implementation of this algorithm in Python.

def smallestdivisor(n):
"""returns the smallest non-trivial divisor of n"""
d = 2 # to begin
while n % d != 0:
d = d+1
return d

def factors(n):
"""returns the prime factorization of n"""
if n == 1:
return [ ] # empty list
else:
p = smallestdivisor(n)
return [p] + factors(n/p)

1
Here’s an example of this code in action, again running in a command
shell, and assuming that the code has been saved into a text file named
factors.py:

$ python
>>> from factors import *
>>> factors(60003)
[3, 3, 59, 113]
>>> factors(1234567)
[127, 9721]
>>> factors(987654321)
[3, 3, 17, 17, 379721]
>>> factors(9721)
[9721]
>>> factors(1234567890123456789)
[3, 3, 101, 3541, 3607, 3803, 27961]
>>>

The function smallestdivisor can be improved, since for instance we never


need to look further than the integer part of the square root for the smallest
divisor of a composite number, and if we get that far without finding a
non-trivial divisor then it proves the number is prime.

Warning: If the input n is a very large prime, then so many divisions need
to be performed that even a supercomputer will take an incredibly long time
to return an answer.

For instance, the number n = 231 − 1 = 2147483647 is prime. This was


shown by Euler in 1772. Using the above code, you will find that it takes
a while to get an answer with this input. If you try the prime num-
ber n = 261 − 1 = 2305843009213693951 as input you may give up be-
fore the code produces an answer. (This prime was discovered by Per-
vushin in 1883.) If that example doesn’t defeat your patience, then try
2127 − 1 = 170141183460469231731687303715884105727, which was shown
prime by Lucas in 1876.
Next we discuss Fermat factorization. The basic idea is to use the factoriza-
tion formula s2 − t2 = (s + t)(s − t) from elementary algebra. If we can find
two integers s, t such that n = s2 − t2 , then by putting a = s − t, b = s + t we
have factored n. Of course, there is no guarantee that either factor a or b is
prime, but if not we could try to factor them, either by repeating Fermat’s
method or by the naive method.

2
To carry out this method, given an odd n, search for perfect squares in the
sequence
u2 − n, (u + 1)2 − n, (u + 2)2 − n, . . .

where u is the smallest integer greater than n. Assuming that n is odd,
this will always work.

Once you have found a perfect square t2 = (u + j)2 − n, then by putting


s = u + j and solving for n you obtain n = s2 − t2 and thus n = ab for
a = s + t, b = s − t.

Example. Here’s an example. To factor n = 110561, we compute n,
which is about 332.5, so we start with u = 333. Assuming that a list of
squares is available, we would find after just 12 steps in the sequence that
(u + 12)2 − n = 922 , so t = 92 and s = u + 12 = 345. Thus we get the
factorization

n = 110561 = (345 + 92)(345 − 92) = 437 · 253.

Fermat compiled lists of squares in order to apply the method, but with a
calculator or computer it is easy enough to see if a given number is a perfect
square.
Fermat’s method is faster than the naive method when n has two factors of
roughly the same number of digits, but it can be very slow in some cases.
The existence of Fermat’s method has implications for modern cryptography.
Here’s Python code that carries out Fermat factorization:

from math import sqrt, ceil


def fermatfactor(n):
"""returns the Fermat factorization of odd n"""
s = int(ceil(sqrt(n)))
while True:
diff = s*s - n
t = int(sqrt(diff))
if t*t == diff:
return s+t, s-t
else:
s = s+1
assert s <= (n+1)/2 # if not, we have a problem

Assuming the code is saved in a file named fermatfactor.py you can run
it in a terminal, as follows:

3
$ python
>>> from fermatfactor import *
>>> fermatfactor(9991013)
(4093, 2441)
>>> fermatfactor(9991013191)
(221231, 45161)
>>> fermatfactor(99910131919153)
(51696809, 1932617)

The code will run the longest time if the input n is actually a prime. In the
case where n = pq is the product of two large primes of the same number
of digits, Fermat factorization should be considerably faster than the naive
method.
The final topic we consider is the distribution of primes. This is a topic that
has attracted the attention of many mathematicians over the years.

Definition. Let π(x) be the number of primes not exceeding x, for any
positive real number x.

Finding a formula for π(x) seems to be an unreasonable expectation. How-


ever, it is an interesting problem to find functions that approximate π(x)
well.

What does it mean for a function f (x) to approximate π(x) well? It is


generally agreed that we want the ratio π(x)
f (x) to approach 1 as x approaches
infinity; i.e., we want the line y = 1 to be a horizontal asymptote for the
ratio π(x)
f (x) . This is equivalent to

π(x)
lim = 1.
x→∞ f (x)

x
Theorem (The Prime Number Theorem). The function f (x) = ln(x) is an
asymptotic approximation for π(x), in the sense that limx→∞ π(x)
f (x) = 1.

This was proved independently by Hadamard and de la Valée-Poussin, in


the year 1886. The proof is far beyond the scope of this course.

In the theorem, the function ln(x) is the natural logarithm function; i.e.,
the inverse of the natural exponential function exp(x) = ex where e =
n
2.718281828459045 . . . is Euler’s constant defined by e = limn→∞ 1 + n1 .

4
It is known that an even better estimate for π(x) is given by the function
Li(x) defined by the following integral:
Z x
dt
Li(x) = .
2 ln(t)

Note that “Li” stands for Logarithmic integral here. Methods of calculus
can be used to compute Li(x).
π(x)
Again, it has been shown that limx→∞ Li(x) = 1, so the function Li(x) is
another asymptotic approximation for π(x).
x
In fact, the function ln(x) provides an underestimate for π(x) while Li(x)
x
proves an overestimate. For example, with x = 1000 we get ln(x) = 144.8
and Li(x) = 178, so π(1000) is somewhere between 145 and 178. In fact,
π(1000) = 168.

You might also like