OFFSET
1,2
COMMENTS
The billiard game considered here is an idealized one: the pool table is a rectangle with vertices (0,0), (p,0), (p,q), (0, q); the ball is shrunk to a point and is launched from vertex (0,0) with initial velocity vector (1,1); collisions are supposed elastic and friction is supposed nonexistent, so that the ball can never stop on the table; when the ball bounces, the angle of reflection is equal to the angle of incidence; the ball can only exit through a vertex.
a(p,q) counts the line segments that constitute the trajectory.
FORMULA
a(p,q) = (p + q) / gcd(p, q) - 1.
EXAMPLE
In a square-shaped pool table, the ball just crosses diagonally. a(p,p)=1.
In a pool table of dimensions 2 X 1, the ball bounces once and exits. a(2,1)=2.
The square array a(p,q) begins:
1 2 3 4 5 6 7
2 1 4 2 6 3 8
3 4 1 6 7 2 9
4 2 6 1 8 4 10
5 6 7 8 1 10 11
6 3 2 4 10 1 12
7 8 9 10 11 12 1
PROG
(Java)
long a(long p, long q) {
long i = 0, x = 0, y = 0, dx = +1, dy = +1, s = 1;
while ((((x % p) != 0) || ((y % q) != 0)) || (i == 0)) {
i ++; long xx = x + dx; long yy = y + dy;
boolean xok = (0 <= xx) && (xx <= p);
boolean yok = (0 <= yy) && (yy <= q);
if (xok && yok) { x = xx; y = yy; }
else { s ++;
if (! xok) { dx = -dx; }
if (! yok) { dy = -dy; }
}} return s; }
CROSSREFS
KEYWORD
nonn,tabl
AUTHOR
Luc Rousseau, Jun 28 2017
STATUS
approved