# MAT 112 Integers and Modern Applications for the Uninitiated

## Section2.4The Assignment let_:=

The instruction let $$a:=b$$ assigns the value of $$b$$ to $$a\text{.}$$ For example after the instruction let $$a:=5\text{,}$$ the variable $$a$$ has the value $$5\text{.}$$ Assume that the variable $$a$$ has the value $$5$$ ; then after let $$a:=a+1\text{,}$$ the variable $$a$$ has the value $$6\text{.}$$ This occurs because we first evaluate that $$a +1$$ is $$6$$ (since $$a = 5$$) and then assign $$6$$ to the variable $$a\text{.}$$
In the video in Figure 2.23 we introduce the assignment instruction let_:= and give first examples.
The assignment instruction let_:= can be used in two ways. We can assign a value to a variable that did not have a value so far or we can assign new value to an existing variable, that we change the value of an existing variable.

### Example2.24.Uses of let _ $$:=$$.

We give examples for the use of the instruction let _ $$:=\text{.}$$
1. The easiest use of let is to assign a value to a variable, for example we can use it to assign the value $$0$$ to $$i$$ :
let $$i:=0$$
After this instruction the value of the variable $$i$$ is $$0\text{.}$$
2. We can also use let to assign the value of an expression to a variable. Assume that the value of the variable $$c$$ is $$10\text{.}$$ Consider the instruction:
let $$d:=c+35$$
First the expression on the right is evaluated. As $$c=10$$ we get that $$c+35=10+35=45\text{.}$$ Then the computed value 45 is assigned to $$d\text{,}$$ so that the value of $$d$$ after this instruction is $$45\text{.}$$
3. It is more confusing when the same variable shows up on both sides of the $$:=$$ of a let instruction. Assume that the value of i is 5 and consider the instruction:
let $$i:=i+1$$
First the expression on the right is evaluated, we obtain $$i+1=5+1=6\text{.}$$ Then the result of this computation, namely 6, is assigned to $$i\text{,}$$ so that the new value of $$i$$ is 6.
The next algorithm computes the same values as Algorithm 2.7, namely $$c\text{,}$$ $$c^2\text{,}$$ $$c^3\text{,}$$ and $$c^4\text{.}$$
We now illustrate how Algorithm 2.25 works with an example.

### Example2.26.Algorithm 2.25 with input $$c=-3$$.

We follow the steps of the Algorithm 2.25 for the input $$c=-3$$
Input $$c=-3$$
1. let $$d:=c\cdot c\text{:}$$ We have $$c=-3\text{.}$$ We compute $$c\cdot c=(-3)\cdot (-3)3= 9$$ and assign this value to the variable $$d\text{.}$$ Now $$d=9\text{.}$$
2. let $$e:=c\cdot d\text{:}$$ We have $$c=-3$$ and $$d=9\text{.}$$ We compute $$c\cdot d=(-3)\cdot 9=-27$$ and assign this value to the variable $$e\text{.}$$ Now $$e=-27\text{.}$$
3. let $$f:=d\cdot d\text{:}$$ We have $$d=9\text{.}$$ We compute $$d\cdot d=9\cdot 9=81$$ and assign this value to the variable $$f\text{.}$$
4. return $$c\text{,}$$ $$d\text{,}$$ $$e\text{,}$$ $$f$$ : We return the values of $$c\text{,}$$ $$d\text{,}$$ $$e\text{,}$$ and $$f\text{,}$$ namely $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ and $$81$$
Output $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ $$81$$
So when the input to Algorithm 2.25 is $$c=-3$$ the output is $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ $$81\text{.}$$
The Input and Output of Algorithm 2.7 and Algorithm 2.25 are the same, but the results are obtained in different ways. In particular the computation effort differs.
• Algorithm 2.7 needs one multiplication to compute $$c^2= c\cdot c\text{,}$$ two multiplications to compute $$c^3= c\cdot c\cdot c\text{,}$$ and three multiplications to compute $$c^4= c\cdot c\cdot c\cdot c$$ (count the multiplication symbols ‘$$\cdot$$’). This is a total of 6 multiplications.
• Algorithm 2.25 computes the same values as $$d=c\cdot c\text{,}$$ $$e=c\cdot d\text{,}$$ and $$f=c^2\cdot c^2\text{.}$$ It only needs 3 multiplications to compute the four values $$d=c^2\text{,}$$ $$e=c^3\text{,}$$ and $$f=c^4\text{.}$$
Because Algorithm 2.25 needs half the number of multiplications that Algorithm 2.7 needs to compute the same values (and no additional operations) Algorithm 2.25 is more efficient than Algorithm 2.7.
Using the same strategy as in Algorithm 2.25 the algorithm in Example 2.27 computes the eighth power of the input. This is the first in a series of interactive algorithms in which you can observe how the instructions change the values of variables by clicking on the instructions.

### Example2.27.Eighth power algorithm interactive.

In Checkpoint 2.28 determine the values returned for by the algorithm for the given input values.

### Checkpoint2.28.Find the output values and determine the output.

Consider the algorithm:
Algorithm
Input: Two integers $$a$$ and $$b\text{.}$$
(1) let $$c:=a+b$$
(2) let $$d:=a\cdot b$$
(3) let $$e:= d-c$$
(4) return $$d$$
What does the algorithm return when the input is $$a=-8$$ and $$b=-6$$ ?
What does the algorithm return when the input is $$a=-8$$ and $$b=10$$ ?
What does the algorithm return when the input is $$a=2$$ and $$b=-7$$ ?
What does the algorithm return when the input is $$a=4$$ and $$b=5$$ ?