In 1971, Edgar Dijkstra published a book named A Discipline of Programming, with the goal of presenting elegant solutions to several programming problems. One of those problems, which he attributed to Richard Hamming, has stuck with me ever since.
Recently I had the occasion to give a talk to a collection of college students and faculty members, so I pulled this problem out of mothballs to revisit it. It turns out to be even more interesting than I had remembered, so I'd like to talk about it here.
The problem statement is surprisingly simple: Write a program that produces, in ascending order, the sequence of positive integers that have only 2, 3, and 5 as their prime factors. The first such integer is 1 (which has no prime factors at all; hence all of its prime factors are either 2, 3, or 5); the sequence continues with 2, 3, 4 (which has only 2 as a prime factor), 5, and 6. It skips 7, which is prime, continues with 8, 9, and 10, skips 11, includes 12, and so on.
As I said, the problem is simple; what's interesting is to write a good solution. For that matter, it's even interesting to discuss what good means in this context. Rather than prejudice you, I'll let you think about the problem for a while and then discuss solutions in future postings. As you think about solutions, I'd like to suggest that you also think about how you might test your solutions.