introduction to computation and programming using python%2c revised - guttag%2c john v..19

Upload: zhichaowang

Post on 04-Nov-2015

222 views

Category:

Documents


0 download

DESCRIPTION

f

TRANSCRIPT

  • 2 Chapter 1. Getting Started

    Consider, for example, finding the square root of 25.

    1. Set g to some arbitrary value, e.g., 3.

    2. We decide that 3*3 = 9 is not close enough to 25.

    3. Set g to (3 + 25/3)/2 = 5.67.3

    4. We decide that 5.67*5.67 = 32.15 is still not close enough to 25.

    5. Set g to (5.67 + 25/5.67)/2 = 5.04

    6. We decide that 5.04*5.04 = 25.4 is close enough, so we stop and declare 5.04 to be an adequate approximation to the square root of 25.

    Note that the description of the method is a sequence of simple steps, together with a flow of control that specifies when each step is to be executed. Such a description is called an algorithm.4 This algorithm is an example of a guess-and-check algorithm. It is based on the fact that it is easy to check whether or not a guess is a good one.

    A bit more formally, an algorithm is a finite list of instructions that describe a computation that when executed on a provided set of inputs will proceed through a set of well-defined states and eventually produce an output.

    An algorithm is a bit like a recipe from a cookbook:

    1. Put custard mixture over heat.

    2. Stir.

    3. Dip spoon in custard.

    4. Remove spoon and run finger across back of spoon.

    5. If clear path is left, remove custard from heat and let cool.

    6. Otherwise repeat.

    It includes some tests for deciding when the process is complete, as well as instructions about the order in which to execute instructions, sometimes jumping to some instruction based on a test.

    So how does one capture this idea of a recipe in a mechanical process? One way would be to design a machine specifically intended to compute square roots. Odd as this may sound, the earliest computing machines were, in fact, fixed-program computers, meaning they were designed to do very specific things, and were mostly tools to solve a specific mathematical problem, e.g., to compute the trajectory of an artillery shell. One of the first computers (built in 1941 by Atanasoff and Berry) solved systems of linear equations, but could do nothing else. Alan Turings bombe machine, developed during World War II, was designed strictly for the purpose of breaking German Enigma codes. Some very simple computers still use this approach. For example, a four-function calculator is a fixed-program computer. It can do basic arithmetic, but it cannot

    3 For simplicity, we are rounding results.

    4 The word algorithm is derived from the name of the Persian mathematician Muhammad ibn Musa al-Khwarizmi.