Programming Pearls
Further Reading
Robert W. Floyd received the ACM Turing Award in
1978. In his Turing lecture on “The Paradigms of
Programming,” Floyd writes, “In my own experience
of designing difficult algorithms, I find a certain
technique most helpful in expanding my own capa-
bilities. After solving a challenging problem, I solve
it again from scratch, retracing only the
insight
of the
earlier solution. I repeat this until the solution is as
clear and direct as I can hope for. Then I look for a
general rule for attacking similar problems, that
would have led me to approach the given problem in
the most efficient way the first time. Often, such a
rule is of permanent value.”
Floyd’s key rule in this problem was, in his own
words, to “look for a mathematical characterization
of the solution before you think about an algorithm
to obtain it.” His key insight dealt with the probabil-
ity of the algorithm generating any particular subset,
When Floyd calculated the probabilities of certain
events in Algorithm S, he noticed that the denomi-
nators were powers of N, while the denominators in
the solution were falling factorials. His algorithms
use a simple structure to generate the correct prob-
abilities. When Floyd finally conceived Algorithm P,
he recalls, “I knew it was right even before I
proved it.”
Floyd’s 1978 Turing lecture was published origi-
nally in the August
1979 Communications.
It also
appears in the
ACM Turing Award Lectures: The First
Twenty Years: 1966-1985,
which was recently pub-
lished by the newly formed ACM Press (a collabora-
tion between ACM and Addison-Wesley).
Solutions to July’s Problems
1. The problem can be rephrased as asking how many
assignments this routine makes after the array
X[l..N] has been sprinkled with random real
numbers.
Max :=
X[l]
for I : =
2
to N do
if X[I]
> Max then
Max := X[I]
A simple argument assumes that
if
statements are
executed about half the time, so the program will
make roughly N/2 assignments. I profiled the pro-
gram for ten runs with N = 1000, and the number
of assignments were, in sorted order: 4, 4, 5, 5,
6, 7, 8, 8, 8, 9. In Section 1.2.10 of
Fundamental Algo-
rithms,
Knuth shows that the algorithm makes
HN
- 1 comparisons, on the average, where
HN =
1+1/2 +1/3 + ...
+ l/N is the Nth harmonic
number. For N = 1000, this analysis gives an expec-
tation of 6.485; the average of the ten experiments
is 6.4.
This C program implements a Sieve of Eratosthenes
for computing all primes less than
n.
main( 1
1
int i, p, n;
char
x[1000023;
n = 100000;
for (i =
1;
i <= n; i++)
x[i] =
1;
x[ll = 0; x[n+ll =
1;
p = 2;
while (p <= n) {
printf ( “%d\n” , p) ;
for (i=2*p; i<=n; i+=p)
x[il = 0;
do
1
1
100000
1
1
9593
9592
9592
256808
9592
99999
99999
I
p++:
while (x[p] == 0);
1
The profile shows that there are 9592 primes less
than 100,000, and that the algorithm made about
2.57N assignments. In general, the algorithm makes
about N log log N comparisons; the analysis involves
the density of prime numbers and the harmonic
numbers mentioned in Solution 1. For faster imple-
mentations of prime sieves,
see
the
Communications
papers by Mairson (September 1977), Gries and
Misra (December 1978) and Pritchard (January
1981).
A simple statement-count profiler increments a
counter at each statement. One can decrease mem-
ory and run time by making do with fewer
counters. For instance, one might associate a coun-
ter with each basic block in the program’s
flow graph. One can further reduce counters by
“Kirchoff’s first law”; if you have a counter for an
if-then-else statement and one for the
then
branch, then you don’t need one for the else
branch.
Problem P6 is a correct program that is hard to
prove correct. The for loop in function prime
could give an infinite loop. To show that it always
terminates, one must prove that if
P
is a prime,
then there is another prime less than
P2.
For Correspondence: Jon Bentley, AT&T Bell Laboratories, Room X-317,
600 Mountain Avenue. Murray Hill, NJ 07974.
Permission to copy without fee all or part of this material is granted
provided that the copies are not made or distributed for direct commer-
cial advantage, the ACM copyright notice and the title of the publication
and its date appear, and notice is given that copying is by permission of
the Association for Computing Machinery. To copy otherwise, or to
republish, requires a fee and/or specific permission.
September 1987 Volume 30 Number 9
Communications of the ACM
757