LearnBuild toward a chatbotMultiplying by a fixed number

Multiplying by a fixed number

Aim. At the end of this lesson your program works out 90 × 100, adds three more products to it, and you know why that costs only eight steps.

Where the lessons are going

The sum starts with four products added into one total. This lesson makes them, in a language that has no "times" command.

A loop that multiplies

You met this loop in lesson 1. The left cell holds a number, and each round adds the same amount to the right cell. Change the number and the amount, and read the right cell at the end: press Play, or drag Step back and forth:

The right cell ends at the left number times the amount: 3 rounds of 4 make 12. Adding the same amount again and again is multiplying. The 4 is not on the tape. It stands in the program, as four plus signs, and that is called a fixed number. A model is full of such multiplications, and the number an input is multiplied by is called a weight. The sliders stop at 9 to keep the run short; the weights of the chat model are larger.

Try it: the left cell holds 3 and the weight is 4. Press Step until the program has ended. What ends up in the right cell?

Figure 1. A loop that multiplies by 4. Watch the right cell grow by four each round. (interactive)

Now 90 × 100

Try it: set the number to 90 and the weight to 100. Go to the last step and read how many steps the program took:

Figure 2. The same loop with your number and weight, run one command at a time. Watch the step counter. (interactive)

The same loop in one step

Try it: press "fast interpreter", then go to the last step. Compare the number of steps with the 9,361 of step 2:

Figure 3. The loop for 90 × 100 with a choice of interpreter. Compare the step counter in the two modes. (interactive)

Four products, one total

Try it: press Step until the program has ended. The total should come to 11,868:

Four inputs on the tape: 90, −25, 40 and 12. Four weights in the program: 100, −60, 30 and 14.

Figure 4. Four products added into one total. Watch each input leave its cell and come back. (interactive)

Write the weight shorter

Try it: this program writes the weight 100 into cell 1 with a hundred plus signs. Do the same with 30 commands or fewer:

Figure 5. A program that writes 100 with a hundred plus signs, for you to shorten. (interactive)

What you can do now

Multiply by a weight that stands in the program, and add products into one total. The first part of the sum is done: 11,868.

11,868 is too large to work with: the model divides it by 128. There is no "divide" command either, and this time no cheap loop. What does dividing cost?

How the real program differs

The fast interpreter of these lessons does three things: a row of + or − is one addition; a row of > or < is one move, done together with the next command; and a loop that only adds and moves, and takes 1 from its own cell each round, is one multiplication. The result is always the same as going one command at a time. The interpreter behind the chat page goes further: it first turns the whole program into the computer's own instructions.

A large fixed number can be written as "a × b + rest" with one loop, which costs about twice the square root of the number in characters. 5,000 then takes 178 characters in place of 5,000.

In step 4 the weights stand in the program. The chat program keeps its 135 million weights on the tape instead; lesson 8 shows how.