CS 70
Discrete Mathematics for CS
Fall 2004
Rao
Lecture 5
The next sequence of lectures is on the topic of
Arithmetic Algorithms
. We shall build up to an understanding
of the RSA publickey cryptosystem.
Primality and Factoring
You are given a natural number — say, 307131961967 — and you are asked:
Is it a prime number?
You
must have faced this familiar kind of question in the past. How does one decide if a given number is prime?
There is, of course, an algorithm for deciding this:
algorithm prime(x)
y := 2
repeat
if x mod y = 0 then return(false);
y := y + 1
until y = x
return(true)
Here by
x
mod
y
we mean the remainder of the division of
x
by
y
negationslash
=
0 (
x
%
y
in the C family). This algorithm
correctly determines if a natural number
x
>
2 is a prime. It implements the definition of a prime number by
checking exhaustively all possible divisors, from 2 up to
x

1. But it is not a useful algorithm: it would be
impractical to apply it even to the relatively modest specimen 307131961967 (and, as we shall see, modern
cryptography requires that numbers with several hundreds of digits be tested for primality). It takes a number
of steps that is proportional to its argument
x
—and, as we shall see, this is bad.
algorithm fasterprime(x)
y := 2
repeat
if x mod y = 0 then return(false);
y := y + 1
until y * y
≥
x
return(true)
Now, this is a little better. This algorithm checks all possible divisors up to the square root of
x
. And this
suffices, because, if
x
had any divisors besides 1 and itself, then consider the smallest among these, and call
it
y
. Thus,
x
=
y
·
z
for some integer
z
which is also a divisor of
x
other than one and itself. And since
y
is
the smallest divisor,
z
≥
y
. It follows that
y
·
y
≤
y
·
z
=
x
, and hence
y
is no larger than the square root of
x
.
And the second algorithm does indeed look for such a
y
.
Still, this algorithm is not satisfactory: in a certain welldefined sense it is “as exponential” as the exhaustive
CS 70, Fall 2004, Lecture 5
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
algorithm for satisfiability. To see why, we must understand how we evaluate the running time of algorithms
with arguments that are natural numbers.
And you know such algorithms: e.g., the methods you learned in elementary school for adding, multiplying,
and dividing whole numbers. To add two numbers, you have to carry out several elementary operations
(adding two digits, remembering the carry, etc.), and the number of these operations is proportional to the
number of digits n
in the input: we express this by saying that the number of such operations is
O
(
n
)
(pro
nounced “bigOh of
n
”). To multiply two numbers, you need a number of elementary operations (looking
up the multiplication table, remembering the carry, etc.) that is proportional to the
square
of the number of
digits, i.e.,
O
(
n
2
)
. (Make sure you understand why it is
n
2
).
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '05
 HUANG
 Algorithms, Natural number, Prime number

Click to edit the document details