Let's consider the example in the drawing above. The source point is (x_{1}, y_{1}) = (2, 3) and the destination point is (x_{2}, y_{2}) = (3, 5). C_{1} and C_{2} in the y-axis are the linear coefficients of the straight lines y = -x + C_{1} and y = -x + C_{2}, which contain these points. From the drawing,

C_{1} = x_{1} + y_{1}; C_{1} = 2 + 3; C_{1} = 5

C_{2} = x_{2} + y_{2}; C_{2} = 4 + 5; C_{2} = 9

The number of steps marked in orange is

n_{1} = C_{2} - C_{1}; n_{1} = 9 - 5; n_{1} = 4

The number of steps marked in blue, from (x

_{1}, y

_{1}) to C

_{1}, is

n_{2} = x_{1}; n_{2} = 2

The number of steps marked in green, from the x-axis to (x

_{2}, y

_{2}), is

n_{3} = y_{2}; n_{3} = 5

The number of the remaining steps, marked in red, is

C_{2}-1

----

n_{4} = \ k ; n_{4} = (C_{2}^{2} - C_{2} - C_{1}^{2} - C_{1})/2; n_{4} = (9^{2} - 9 - 5^{2} - 5)/2; n_{4} = 21

/

----

k=C_{1}+1

Finally, the total number of steps is

n = n_{1} + n_{2} + n_{3} + n_{4}; n = 4 + 2 + 5 + 21; n= 32

or, generically,

n = C_{2} - C_{1} + x_{1} + y_{2} + (C_{2}^{2} - C_{2} - C_{1}^{2} - C_{1})/2

which can be simplified to

n = ((x_{2} + y_{2})^{2} + (x_{2} + y_{2}) - ((x_{1} + y_{1})^{2} + (x_{1} + y_{1})))/2 + y2 - y1

The symetry of the latter allows for this HP 50g 55-byte program:

PGM1:

« ROT DUP2 - 5 ROLLD

1 2

START

UNROT + DUP SQ +

NEXT

- 2 / +

»

Example:

2 ENTER 3 ENTER 4 ENTER 5 PGM1 --> 32

This doesn't mean, however, smaller RPL programs are not possible.

Gerson.

*Edited: 25 Sept 2012, 9:17 p.m. *