# Mad robot

Can you work out what happens when this mad robot sets off?

## Problem

A mad robot sets off towards the North East on a journey from the origin. It travels in stages by moving forward and then rotating on the spot. It follows these pseudo-code instructions:

SUB JOURNEY

$\quad$DISTANCE = 1000

$\quad$WHILE (DISTANCE > 0.001)

$\quad\quad$MOVE DISTANCE

$\quad\quad$STOP

$\quad\quad$ROTATE(90, DEGREES, CLOCKWISE)

$\quad\quad$DISTANCE = DISTANCE / 2

$\quad$END WHILE

$\quad$EXPLODE

END SUB

Where does the robot explode?

Extension: Write a program such that the angle rotated and other factors in the problem are variables. Explore where the robot might end up.

Did you know ... ?

Many computer programming jobs involve a mixture of logic, clear thinking and mathematics. Learning lots of mathematics and learning to think programmatically can open many doors in the job-market.

Many computer programming jobs involve a mixture of logic, clear thinking and mathematics. Learning lots of mathematics and learning to think programmatically can open many doors in the job-market.

## Student Solutions

Our thanks go to azerbajdzan for correcting an error in the original solution to this challenge

The first part of this journey consists of a sequence of forward motions followed by 90 degree turns. Although we could just halve repeatedly to find the number of turns, we might consider the possibility that the numbers in the problems might change, so look for a general proceduce for finding the number of motions.

In doing this, we need to be careful about the number of forward turns and note that the WHILE(DISTANCE > 0.001) condition is evaluated after each halving.

The forward motion, if it occurs, following the $n$th turn will be of distance $1000\times 2^{-n}$.This will occur for each $n$ for which

$1000\times 2^{-n}> 0.001$

Taking logs gives

$\log(1000) -n\log(2)> \log(0.001)$

Rearranging gives

$n < \frac{\log(1000)-\log(0.001)}{\log(2)}=\frac{\log(1000000)}{\log{(2)}}\approx 19.93$

This first becomes invalid AFTER after 20 turns. Thus, the path of the robot is determined by $20$ motions. The directions that the robot travels in these 20 motions are alternately NE, SE, SW, NW, .... Since there are 20 motions in total there are 5 motions in each of these directions.

Let $T$ be the total distance travelled NE. Then

$T= 1000\left(\frac{1}{2^0}+\frac{1}{2^4}+\frac{1}{2^8}+\frac{1}{2^{12}}+\frac{1}{2^{16}}\right)$

Note that the distance SE will be half of this, since each motion is half the length of the preceding NE motion. Similarly, total distance SW will be one quarter of $T$; total distance NW will be one eight of $T$. Each of these motions contributes a component in the $x$-direction and component in the $y$-direction.

The final coordinates will therefore be

$$

(x, y) = \left(\frac{T}{\sqrt{2}}\left(1+\frac{1}{2}-\frac{1}{4}-\frac{1}{8}\right),\frac{T}{\sqrt{2}}\left(1-\frac{1}{2}-\frac{1}{4}+\frac{1}{8}\right)\right)=\left(\frac{9T}{8\sqrt{2}}, \frac{3 T}{8\sqrt{2}}\right)

$$

## Teachers' Resources

This short problem can be given to students who have been working on sequences or algorithms.