../
How I Spent a Weekend Watching a Ball Bounce (Project Euler #786)
authored by aniketh

The Problem

So Project Euler #786 gives you a billiard table. It's a nice kite with angles 120°120°, 90°90°, 60°60°, 90°90° at corners AA, BB, CC, DD.

Really simple problem statement honestly; You shoot a ball from corner AA, it bounces around with perfect physics and you want it to come back to AA. How many distinct paths can the ball take if it bounces at most NN times?

To sanity check your work: with up to 1010 bounces, there are only 66 valid paths. With 100100, there's 478478. A thousand gives you 45,79045{,}790.

Y'all just need to find the number of valid paths when N=109N = 10^9. Yeah. 10910^9.

Billiard traces showing ball paths on the quadrilateral table
Billiard traces showing ball paths on the quadrilateral table

Why This One?

The first Euler problem I ever solved was #785 (I think it was February 2022?), the one right before this. It was a Diophantine equation problem; took me about 44-55 hours. The high was insane when my name appeared on the leaderboard (<100100 solves in a week!) and naturally I looked at the next one.

#786 was a completely different beast. I stared at it for a while and genuinely didn't know where to start.

I eventually found a similar older problem after lots of digging on periodic trajectories (pre-ChatGPT and google was ass), #202 (Laserbeam), which is also about counting reflections inside a geometric shape and I ended up solving that one first (you'll soon know why)

In short, it's just me documenting the approach / solution to my favorite problem which I solved back in February 2022 lol.

First Instinct

My honest first thought was to simulate it. Just trace the ball and count the bounces, see if I could find any patterns for small NN (hoping to find some obscure pattern on OEIS to cheat my way onto the leaderboard)

I wrote a quick script that does exactly that and check this out; drag the slider to the projected paths as you allow more bounces:

N = 20

All I got were beautiful patterns instead lol (surely I'm missing something; check how circles form beautifully with increasing NN!)

What if we stop watching the ball?

Try setting the slider to 22. Look at the simplest path: it leaves AA, hits two edges, and comes back.

Now what if I were to trace each edge of trajectory in a mirror fashion? Take a look at the video below to understand.

A straight line. Every bouncing path is a straight line if you unfold the billiard table enough times. Mind map the above with a larger billiard trajectory and you'd find that the setting is a infinitesimal hexagonal lattice when you keep reflecting the trajectory edges.

The question now sounds different; we no longer care where the ball bounces. Stare at it a bit and it turns into "how many straight lines from the origin to lattice points cross at most NN cell boundaries?"

A number theory problem in disguise. Heh.

Where It Gets Hard

We've narrowed it down to the right problem statement - a bit of observation leads us to three inferences:

1. Not every lattice point is corner AA: When you map out the plane with reflections, some points correspond to AA, some to BB, some to CC, some to DD. We've started at A so we only want the ones where the ball actually returns to AA.

2. The path has to be "direct": If a straight line passes through another copy of AA on its way to the target, that means it's a shorter path; Think about it: the ball would've returned to AA earlier. So we need some way to filter these cases out.

3. Counting bounces from coordinates: Playing around with the simulations a bit led me to an unproven hunch that the bounce count for a path to some target AA' in the lattice kept matching with the number of edges the straight line intersects in the lattice.

Piecing It Together

The first useful thing I found was Baxter & Umble's paper on periodic billiard orbits in triangles along with a couple more similar papers on connectedpapers.com. It kinda unfolds what we're trying to do but for equilateral triangles instead (which problem #202 was about). I tried to adapt their approach to our hexagonal lattice but it didn't really line up with what I was thinking.

The paper focused on developing a coordinate system for equilateral triangle lattice structure but you just tweak it a bit with some vector arithmetic and we have our own thing now.

For Inference 1, Say we represent each target as a point (a,b)(a, b) using two basis vectors 2AB2 \cdot \overrightarrow{AB} and 2AD2 \cdot \overrightarrow{AD}. It is straightforward that not every (a,b)(a, b) gets us back to AA. the tiling has 3 types of vertices (AA, B/DB/D midpoints, CC centroid).

Revisiting our simulation of small cases, we find a nice and clean pattern (phew it wasn't a waste of effort): (a,b)(a, b) lands on AA when a+b0(mod3)a + b \equiv 0 \pmod{3}. The rest land on B/DB/D midpoints (a+b1(mod3)a + b \equiv 1 \pmod{3}) or on CC (a+b2(mod3)a + b \equiv 2 \pmod{3}).

Inference 2 is straightforward honestly; Coprime pairs. Basically mm and nn are coprime if the only positive integer that is a divisor of both of them is 11. Straight from wikipedia.

Inference 3 feels the hardest; and it is. We'll see why soon.

Counting edge crossings

We've been dangling on inference 3 for a while; lets do it.

Lemma 1 (Crossing Lemma). Let F\mathcal{F} be a family of parallel lines with spacing ss. A segment whose projection onto the perpendicular direction has length LL crosses L/s\lfloor L/s \rfloor lines of F\mathcal{F}.

In plain terms, a segment from the origin to (p,q)(p, q) crosses p|p| vertical grid lines because its xx-coordinate goes from 00 to pp and the grid spacing is 11. We'll use this lemma repeatedly to get to our theorem.

Two kinds of edges

When we unfold the billiard table, the tiling has two kinds of edges:

Two types of edges in the hexagonal tiling
Two types of edges in the hexagonal tiling

The purple hexagon edges are copies of sides ABAB and ADAD from our original table. The teal internal edges are copies of sides BCBC and CDCD. A straight line from the origin to (a,b)(a, b) crosses both kinds, and we count them separately.

But why two kinds? If you observe closely, each equilateral triangle in the underlying triangular lattice is subdivided into 33 copies of our quadrilateral ABCDABCD. The vertex AA sits at a triangle vertex, CC at the centroid and BB, DD at edge midpoints. The edges BCBC and CDCD (centroid to midpoint) lie along the triangular lattice lines. The edges ABAB and ADAD (vertex to midpoint) form the hexagonal boundaries around each AA-vertex.

Part 1: Internal (triangle-grid) edges

Let's take a look at the tiling diagram again. The internal edges (teal) lie along three directions; BCBC and CDCD segments connecting centroids to edge midpoints.

Definition 1. Let s=AB=(1,0)\vec{s} = \overrightarrow{AB} = (1, 0) and t=AD=(12,32)\vec{t} = \overrightarrow{AD} = (-\tfrac{1}{2}, \tfrac{\sqrt{3}}{2}), which sit at 120°120° to each other (the angle at AA). The target point for lattice coordinates (a,b)(a, b) is:

P=2as+2bt=(2ab,  b3)P = 2a\vec{s} + 2b\vec{t} = (2a - b,\; b\sqrt{3})

We need actual numbers to work with so we drop our basis vectors into Cartesian coordinates. The 120°120° angle between s\vec{s} and t\vec{t} gives us t\vec{t}'s components.

Proposition 1. The projections of PP onto the three perpendicular directions are:

Ps^=2ab,Pt^=2ba,P(s+t)^=a+bP \cdot \hat{s} = 2a - b, \quad P \cdot \hat{t} = 2b - a, \quad P \cdot \widehat{(s+t)} = a + b

Proof. Projection onto s^=(1,0)\hat{s} = (1, 0): trivially 2ab2a - b. Onto t^=(12,32)\hat{t} = (-\tfrac{1}{2}, \tfrac{\sqrt{3}}{2}):

(2ab) ⁣(12)+b332=a+b2+3b2=2ba(2a - b)\!\left(-\tfrac{1}{2}\right) + b\sqrt{3} \cdot \tfrac{\sqrt{3}}{2} = -a + \tfrac{b}{2} + \tfrac{3b}{2} = 2b - a

Onto (s+t)^=(12,32)\widehat{(s+t)} = (\tfrac{1}{2}, \tfrac{\sqrt{3}}{2}):

(2ab) ⁣(12)+b332=ab2+3b2=a+b(2a - b)\!\left(\tfrac{1}{2}\right) + b\sqrt{3} \cdot \tfrac{\sqrt{3}}{2} = a - \tfrac{b}{2} + \tfrac{3b}{2} = a + b

We’ve got three numbers: 2ab2a - b, 2ba2b - a, and a+ba + b. Each one is basically “how far” our line from the origin to PP goes in one of the three perpendicular directions to the internal edges. These are exactly the LL’s we need for the crossing lemma which are one for each edge direction.

Lemma 2. The internal edges are spaced 33 units apart in (a,b)(a, b) coordinates.

Take another look at the tiling diagram. Pick any AA-vertex of your choice. The nearest other AA-vertex is not the next cell over because there are 2 other cells (a B/DB/D-type and a CC-type) between them. So to reach from one AA to the next AA in any direction, you'll always cross exactly 3 cells. That's why the edge spacing is 3 in our coordinates.

Corollary 1. By Lemma 1 (projection ÷\div spacing), the number of internal edge crossings is:

2ab3+2ba3+a+b3\frac{|2a - b|}{3} + \frac{|2b - a|}{3} + \frac{a + b}{3}

Corollary 2 (Integrality). These are integers whenever a+b0(mod3)a + b \equiv 0 \pmod{3}, since 2ab=3a(a+b)2a - b = 3a - (a+b) and 2ba=3b(a+b)2b - a = 3b - (a+b) are both divisible by 33.

Part 2: Hexagon edges

The hexagon edges (purple in the diagram) are the ABAB and ADAD segments. Same deal as the above; you've got three families, one per direction. But this is a bit tricky because unlike the internal edges which run across the entire plane as continuous lines, hexagon edges are short segments that only show up at certain spots.

Lemma 3. Hexagon edges parallel to s\vec{s} repeat with period 33 in the bb-coordinate. Similarly, those parallel to t\vec{t} repeat with period 33 in the aa-coordinate, and those parallel to s+t\vec{s} + \vec{t} repeat with period 33 in ab|a - b|.

Part 1's internal edges were continuous lines running across the entire plane so dividing the projection by the spacing give us clean integer counts. But the hexagon edges are different since they're short segments that only exist around AA-vertices. Since AA-vertices sit 33 cells apart, these segments show up every 33 rows. But whether our line actually hits one depends on where exactly it crosses that row. Some rows it hits a hexagon edge, some rows it slips through the gap.

Corollary 3. By Lemma 1, the number of hexagon edge crossings is:

a3+b3+ab3\left\lfloor \frac{a}{3} \right\rfloor + \left\lfloor \frac{b}{3} \right\rfloor + \left\lfloor \frac{|a - b|}{3} \right\rfloor

The floor functions (instead of clean division like Part 1) are because not every row has a hexagon edge; they're discrete segments and aren't continuous lines.

Total bounce count

Theorem 1. The total number of bounces for a path to lattice point (a,b)(a, b) is:

bounces(a,b)=2ab3+2ba3+a+b3internal edges+a3+b3+ab3hexagon edges\text{bounces}(a, b) = \underbrace{\frac{|2a-b|}{3} + \frac{|2b-a|}{3} + \frac{a+b}{3}}_{\text{internal edges}} + \underbrace{\left\lfloor\frac{a}{3}\right\rfloor + \left\lfloor\frac{b}{3}\right\rfloor + \left\lfloor\frac{|a-b|}{3}\right\rfloor}_{\text{hexagon edges}}

What's Next?

Honestly I'd like to take a jab at other polygons. This problem is kinda well researched for Triangles and their derivatives (our quadrilateral was just 2 equilateral triangles stuck together) but I'm not entirely sure how it's going to work out with other stuff.

Squares and Rectangles feel straightforward, maybe there's some generalization to n-gon's since there's some lattice repeatability? Maybe I'll stop at this; hopefully there's a part 2 where I get to extend this.

Props to Axiom Math's APIs for verifying this!

References

[1] A. Baxter, R. Umble. "Periodic orbits of billiards on an equilateral triangle." The American Mathematical Monthly, 2008. https://arxiv.org/abs/math/0509292

[2] "Euclid's orchard." Wikipedia. https://en.wikipedia.org/wiki/Euclid%27s_orchard

[3] Project Euler. "Problem 786: Billiard." https://projecteuler.net/problem=786

[4] Project Euler. "Problem 202: Laserbeam." https://projecteuler.net/problem=202

Additional reading

[5] R. Becker. "Periodic billiard trajectories in isosceles right-angled triangles." 2013. https://arxiv.org/abs/1306.6702

[6] R. Becker. "On the local theory of billiards in polygons." 2014. https://arxiv.org/abs/1405.1150

[7] Connected papers graph for [1]. https://www.connectedpapers.com/main/34f03b3e8a4ec20176678be6a596f043daffac99/Periodic-Orbits-for-Billiards-on-an-Equilateral-Triangle/graph

aniketh profile picture
Find aniketh on:linkedin: https://www.linkedin.com/in/aniketh-reddimitwitter: https://x.com/thekingofxorgithub: https://github.com/p4r4xoremail: hi@paraxor.devdiscord: truelights