Where will this robot be after n iterations?
Question
A particular robot has three commands:
-
F: Move forward a unit distance;
-
L: Turn left 90∘;
-
R: Turn right 90∘.
A program is a sequence of commands. We consider particular programs Pn (for n≥0) in this question. The basic program P0 just instructs the robot to move forward: P0=F.
The program Pn+1 (for n≥0) involves performing Pn, turning left, performing Pn again, then turning right: Pn+1=PnLPnR.
So, for example, P1=FLFR.
- Write down the program P2.
- How far does the robot travel during the program Pn? In other words, how many F commands does it perform?
- Let ln be the total number of commands in Pn; so, for example, l0=1 and l1=4. Write down an equation relating ln+1 to ln. Hence write down a formula for ln in terms of n. No proof is required. Hint: consider ln+2.
- The robot starts at the origin, facing along the positive x-axis. What direction is the robot facing after performing the program Pn?
- The first diagram below shows the path the robot takes when it performs the program P1. On the second (blank) diagram, draw the path it takes when it performs the program P4.
- Let (xn,yn) be the position of the robot after performing the program Pn, so (x0,y0)=(1,0) and (x1,y1)=(1,1). Give an equation relating (xn+1,yn+1) to (xn,yn). What is (x8,y8)? What is (x8k,y8k)?


Solution
By the (recursive) definition of
Pn
,
P2=P1LP1R=(FLFR)L(FLFR)R=FLFRLFLFRR.
Let the number of F commands in Pn be fn. Then since Pn+1=PnLPnR, we have fn+1=2fn. As f0=1, this gives fn=2n.
- Let ln be the total number of commands in Pn; so, for example, l0=1 and l1=4. Write down an equation relating ln+1 to ln. …
If ln is the total number of commands in Pn, then since Pn+1=PnLPnR, we have ln+1=ln+1+ln+1=2ln+2.
… Hence write down a formula for ln in terms of n. No proof is required. Hint: consider ln+2.
Let Ln=ln+2, following the suggestion. Using our formula for ln, we find Ln+1=ln+1+2=2ln+2+2=2Ln, and therefore, Ln=2nL0.
We are given l0=1, so L0=3, so Ln=3×2n.
Then as ln=Ln−2, we get ln=3×2n−2.
Check: for n=2, this gives l2=3×4−2=10, which tallies.
Since P0 contains only an F, we see that each program Pn contains an equal number of L and R commands.
Therefore the robot will still face along the positive x-axis after completing Pn.
It helps to work out the paths for P2 and P3.
To find P3, we do P2, then turn to north and repeat P2.
To find P4, we do P3, then turn to north and repeat P3. This gives us
Four copies of P2 are clearly visible, as are 8 copies of P1.
We have the relationship Pn+1=PnLPnR.
Originally the robot is at the origin facing east. Performing Pn takes the robot to (xn,yn) still facing east.
The move L turns the robot left 90∘, so it then faces north.
Now imagine the robot is at the origin but facing north. Performing Pn moves the robot to position (−yn,xn).
So facing north and performing Pn will translate the robot by the vector (xn−yn).
So starting at the origin and facing east, after performing PnLPn, the robot will be at position (xn−yn,yn+xn).
This position is obviously unchanged by performing the final R.
Therefore (xn+1,yn+1)=(xn−yn,xn+yn).
By using the relationship we have just established, we find that
(xn+2,yn+2)=(xn+1−yn+1,xn+1+yn+1)=(xn−yn−xn−yn,xn−yn+xn+yn)=(−2yn,2xn).
Therefore,
(xn+4,yn+4)=(−2yn+2,2xn+2)=(−4xn,−4yn)
and so
(xn+8,yn+8)=(−4xn+4,−4yn+4)=(16xn,16yn).
Setting n=0 in the final expression gives (x8,y8)=(16x0,16y0)=(16,0), and we can also see from the final expression that (x8k,y8k)=(16k,0).
The program Logo is perfect for trying out programs involving F, L and R. Search on the internet to find a free download.



