Menu

- Chapter 1 Distilled
- Introduction
- 1.1 - The Elements Of Programming
- Expressions
- Naming and the Environment
- Evaluating Combinations
- Defining New Functions
- The Substitution Model
- Exercises
- Predicates
- Conditional Expressions
- Example: Newton’s Method
- Functions as Black-Box Abstractions
- 1.2 - Procedures and the Processes They Generate
- Linear Recursion and Iteration
- Tree Recursion
- Orders of Growth
- Exponentiation
- Example: Testing For Primality
- 1.3 Higher Order Functions
- Project - Blackjack

- Chapter 2 Distilled
- Introduction
- Data Abstraction
- Everything From Nothing
- Abstractions In Clojure
- Clojure's Data Structures
- Data Abstraction, Revisited
- Escher
- Project - Escher
- Symbolic Data
- Representing Sets
- Huffman Encoding Trees
- Zippers

This section describes two methods for checking the primality of an
integer `n`

, one with order of growth $ \Theta(n) $, and a
“probabilistic” algorithm with order of growth $ \Theta(\log n) $.

Since ancient times, mathematicians have been fascinated by problems concerning prime numbers, and many people have worked on the problem of determining ways to test if numbers are prime. One way to test if a number is prime is to find the number’s divisors. The following program finds the smallest integral divisor (greater than 1) of a given number n. It does this in a straightforward way, by testing n for divisibility by successive integers starting with 2.

```
(defn smallest-divisor [n]
(find-divisor n 2))
(defn find-divisor [n test-divisor]
(cond (> (square test-divisor) n) n
(divides? test-divisor n) test-divisor
:else (find-divisor n (+ test-divisor 1))))
(defn divides? [a b]
(= (remainder b a) 0))
```

We can test whether a number is prime as follows: n is prime if and only if n is its own smallest divisor.

```
(defn prime? [n]
(= n (smallest-divisor n)))
```

The end test for find-divisor is based on the fact that if n is not prime it must have a divisor less than or equal to $\sqrt n $. This means that the algorithm need only test divisors between $1$ and $\sqrt n $. Consequently, the number of steps required to identify $n$ as prime will have order of growth $ \Theta(\sqrt n) $

The $ \Theta(\log n) $ primality test is based on a result from number theory

If n is a prime number and a is any positive integer less than n, then a raised to the n^{th}power is congruent to a modulo n. Fermat’s Little Theorem

(Two numbers are said to be congruent modulo n if they both have the same remainder when divided by n. The remainder of a number a when divided by n is also referred to as the remainder of a modulo n, or simply as a modulo n.)

If n is not prime, then, in general, most of the numbers $a \lt n$ will not satisfy the above relation. This leads to the following algorithm for testing primality:

Given a number n, pick a random number $a \lt n$ and compute the remainder of $ a^n $ modulo n.

- If the result is not equal to a, then n is certainly not prime.
- If it is a, then chances are good that n is prime.

Now pick another random number a and test it with the same method.

- If it also satisfies the equation, then we can be even more confident that n is prime.

By trying more and more values of a, we can increase our confidence in
the result. This algorithm is known as the *Fermat test*.

To implement it, we need a procedure that computes the of exponential a number modulo another number:

```
(defn expmod [base exp m]
(cond (= exp 0)
1
(even? exp)
(rem (square (expmod base (/ exp 2) m))
m)
:else
(rem (* base (expmod base (dec exp) m))
m)))
```

This is very similar to the `fast-expt`

function earlier. It uses
successive squaring, so that the number of steps grows logarithmically
with the exponent. The Fermat test is performed by choosing at random
a number a between $1$ and $n−1$ inclusive and checking whether the
remainder modulo $n$ of the $n^\text{th}$ power of $a$ is equal to
$a$. The random number a is chosen using the procedure `rand-int`

, a
Clojure built-in.

`rand-int`

returns a nonnegative integer less than its integer
input. Hence, to obtain a random number between `1`

and `n − 1`

, we call
random with an input of `n − 1`

and add `1`

to the result:

```
(defn fermat-test [n]
(let [a (inc (rand-int (dec n)))]
(= (expmod a n n) a)))
```

The following procedure runs the test on `n`

, repeating `num-tests`

times. Its value is true if the test succeeds every time, and false
otherwise.

```
(defn fast-prime?
([n] (fast-prime? n 50))
([n num-tests]
(every? true? (take num-tests (repeatedly #(fermat-test n))))))
```

We have used some of Clojure's sequence functions here, reading from right to left:

`fast-prime?`

`repeatedly`

runs `#(fermat-test n)`

, takes `num-tests`

of them, sees if `every?`

one of them is `true?`

(`every?`

short-circuits, if any of them are false it has failed the
test and will be reported as non-prime)

The Fermat test differs in character from most familiar algorithms, in which one computes an answer that is guaranteed to be correct. Here, the answer obtained is only probably correct. More precisely, if n ever fails the Fermat test, we can be certain that $n$ is not prime. But the fact that $n$ passes the test, while an extremely strong indication, is still not a guarantee that $n$ is prime. What we would like to say is that for any number n, if we perform the test enough times and find that $n$ always passes the test, then the probability of error in our primality test can be made as small as we like.

Unfortunately, this assertion is not quite correct. There do exist numbers that fool the Fermat test: numbers $n$ that are not prime and yet have the property that a $n$ is congruent to a modulo $n$ for all integers $a \lt n$. Such numbers are extremely rare, so the Fermat test is quite reliable in practice.

There are variations of the Fermat test that cannot be fooled. In these tests, as with the Fermat method, one tests the primality of an integer $n$ by choosing a random integer $a \lt n$ and checking some condition that depends upon $n$ and a. (See Exercise 1.28 for an example of such a test.) On the other hand, in contrast to the Fermat test, one can prove that, for any n, the condition does not hold for most of the integers $a \lt n$ unless $n$ is prime. Thus, if $n$ passes the test for some random choice of a, the chances are better than even that $n$ is prime. If $n$ passes the test for two random choices of a, the chances are better than 3 out of 4 that $n$ is prime. By running the test with more and more randomly chosen values of a we can make the probability of error as small as we like.

The existence of tests for which one can prove that the chance of error becomes arbitrarily small has sparked interest in algorithms of this type, which have come to be known as probabilistic algorithms. There is a great deal of research activity in this area, and probabilistic algorithms have been fruitfully applied to many fields.