Algorithms
----------
.. index::
   single: algorithms

.. index::
   single: algorithm

When you write a general solution for a class of problems, as opposed to
a specific solution to a single problem, you have written an
**algorithm**. I mentioned this word in Chapter 1, but did not define it
carefully. It is not easy to define, so I will try a couple of
approaches.

First, consider something that is not an algorithm. When you learned to
multiply single-digit numbers, you probably memorized the multiplication
table. In effect, you memorized 100 specific solutions. That kind of
knowledge is not really algorithmic.

But if you were “lazy,” you probably cheated by learning a few tricks.
For example, to find the product of :math:`n` and 9, you can write
:math:`n-1` as the first digit and :math:`10-n` as the second digit.
This trick is a general solution for multiplying any single-digit number
by 9. That’s an algorithm!

Similarly, the techniques you learned for addition with carrying,
subtraction with borrowing, and long division are all algorithms. One of
the characteristics of algorithms is that they do not require any
intelligence to carry out. They are mechanical processes in which each
step follows from the last according to a simple set of rules.

In my opinion, it is embarrassing that humans spend so much time in
school learning to execute algorithms that, quite literally, require no
intelligence.

On the other hand, the process of designing algorithms is interesting,
intellectually challenging, and a central part of what we call
programming.

Some of the things that people do naturally, without difficulty or
conscious thought, are the most difficult to express algorithmically.
Understanding natural language is a good example. We all do it, but so
far no one has been able to explain *how* we do it, at least not in the
form of an algorithm.

Later in this book, you will have the opportunity to design simple
algorithms for a variety of problems. If you take the next class in the
Computer Science sequence, Data Structures, you will see some of the
most interesting, clever, and useful algorithms computer science has
produced.

.. fillintheblank:: algorithms_1

    When you write a general solution for a class of problems, as opposed to a specific solution to a single problem, you have written an ____.

    - :(?:a|A)lgorithm: Correct!
      :.*: Try again!
