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?
What you just saw
Every round took 1 from the left cell and added 4 to the right cell. Three rounds: 3 × 4 = 12.
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:
What you just saw
The answer is right: 9,000. The weight is a row of a hundred plus signs in the program.
But it took 9,361 steps. Every round is 104 commands, and there are 90 rounds. A model that needs millions of products cannot pay that.
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:
What you just saw
The same program, the same answer, one step.
Whatever carries out the commands is called an interpreter. A simple one does one command at a time. A fast one first looks at the loop. It sees that the loop only adds and moves, so it does the whole loop as one multiplication: "add 100 times this cell to that cell, and empty this cell".
The chat page uses a fast interpreter. From here on, "steps" are counted the fast way. Simple loops like this one are the cheap building blocks.
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.
What you just saw
Each input was poured into the total, times its weight. At the same time it was poured into a scratch cell, and from there back. That is the copy of step 2 of lesson 2. Two steps per input, eight in all.
A weight below zero is a row of minus signs. Nothing else changes.
This is the first part of the sum: 90 × 100 + (−25) × (−60) + 40 × 30 + 12 × 14 = 11,868.
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:
Hint
10 × 10 is also 100. Put 10 in cell 0. Then a loop that takes 1 from cell 0 and adds 10 to cell 1, as in step 1.
What you just saw
You traded plus signs for a loop. A number in a program costs characters, and a big number costs many.
Keep that in mind for lesson 8. The chat model has 135 million numbers.
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.
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.