Project Euler Problem 144

In laser physics, a "white cell" is a mirror system that acts as a delay line for the laser beam.

Project Euler Problem 144

Solution

Answer: 354

We model the laser beam as a sequence of straight-line segments reflecting inside the ellipse

$$4x^2+y^2=100.$$

We are asked to count how many times the beam hits the internal surface before it exits through the tiny opening at the top:

$$-0.01 \le x \le 0.01,\qquad y>0.$$

The beam starts outside at $(0,10.1)$ and first strikes the ellipse at

$$(1.4,-9.6).$$


Mathematical analysis

1. Geometry of reflection

For the ellipse

$$4x^2+y^2=100,$$

implicit differentiation gives the tangent slope:

$$8x+2y\frac{dy}{dx}=0$$

so

$$\frac{dy}{dx}=-\frac{4x}{y}.$$

Thus at a point $(x,y)$:

  • tangent vector can be taken as

$$T=(1,-4x/y),$$

  • normal vector is perpendicular to this.

A cleaner approach is to use the gradient of the ellipse:

$$F(x,y)=4x^2+y^2.$$

Then

$$\nabla F=(8x,2y)$$

is normal to the ellipse.

So at impact point $P=(x,y)$, a normal vector is

$$N=(4x,y).$$


2. Vector reflection formula

Suppose the incoming direction vector is $v$.

Reflection across a normal $N$ is:

$$v_{\text{out}} = v - 2\frac{v\cdot N}{N\cdot N}N.$$

This avoids trigonometry entirely and is numerically stable.


3. Finding the next intersection

Suppose the beam leaves point $P=(x_0,y_0)$ with direction $v=(d_x,d_y)$.

The parametric line is

$$(x,y)=(x_0,y_0)+t(d_x,d_y).$$

Substitute into the ellipse equation:

$$4(x_0+t d_x)^2+(y_0+t d_y)^2=100.$$

Because $P$ already lies on the ellipse, one root is $t=0$.

The second root gives the next impact point.

Expanding:

$$4(x_0^2+2x_0d_xt+t^2d_x^2) +y_0^2+2y_0d_yt+t^2d_y^2 =100.$$

Using $4x_0^2+y_0^2=100$,

$$t(8x_0d_x+2y_0d_y) +t^2(4d_x^2+d_y^2)=0.$$

Hence the nonzero root is

$$t = -\frac{8x_0d_x+2y_0d_y} {4d_x^2+d_y^2}.$$

Then

$$P_{\text{next}}=P+t,v.$$


Python implementation

import math

# Starting point outside the ellipse
x_prev, y_prev = 0.0, 10.1

# First impact point
x, y = 1.4, -9.6

# Count reflections on the internal surface
count = 0

while True:

    # Check whether beam exits through the hole
    if abs(x) <= 0.01 and y > 0:
        break

    # Incoming direction vector
    vx = x - x_prev
    vy = y - y_prev

    # Normal vector to ellipse at (x, y)
    nx = 4 * x
    ny = y

    # Reflect v across the normal
    dot = vx * nx + vy * ny
    norm_sq = nx * nx + ny * ny

    rx = vx - 2 * dot / norm_sq * nx
    ry = vy - 2 * dot / norm_sq * ny

    # Find next intersection with ellipse
    t = -(8 * x * rx + 2 * y * ry) / (4 * rx * rx + ry * ry)

    x_next = x + t * rx
    y_next = y + t * ry

    # Advance
    x_prev, y_prev = x, y
    x, y = x_next, y_next

    count += 1

print(count)

Code walkthrough

Initial conditions

x_prev, y_prev = 0.0, 10.1
x, y = 1.4, -9.6

The beam begins outside the ellipse and first strikes at $(1.4,-9.6)$.


Exit condition

if abs(x) <= 0.01 and y > 0:
    break

The beam escapes only through the tiny opening near the top.


Incoming direction

vx = x - x_prev
vy = y - y_prev

This is the direction from the previous point to the current point.


Surface normal

nx = 4 * x
ny = y

Because the ellipse is

$$4x^2+y^2=100,$$

the gradient $(8x,2y)$ is normal; scaling by $1/2$ gives $(4x,y)$.


Reflection step

dot = vx * nx + vy * ny
norm_sq = nx * nx + ny * ny

rx = vx - 2 * dot / norm_sq * nx
ry = vy - 2 * dot / norm_sq * ny

This implements the vector reflection formula.


Next intersection

t = -(8 * x * rx + 2 * y * ry) / (4 * rx * rx + ry * ry)

This computes the nonzero intersection parameter with the ellipse.

Then:

x_next = x + t * rx
y_next = y + t * ry

gives the next bounce point.


Small sanity check

The first reflection computed numerically is approximately

$$(-3.9906,-6.025)$$

which matches known trajectories for this problem and the animation shown in the statement.

The beam remains trapped for many reflections because the ellipse focuses trajectories efficiently.


Final verification

Running the simulation carefully with floating-point arithmetic gives:

$$354$$

internal reflections before the beam exits through the opening.

This magnitude is reasonable: the beam undergoes hundreds of reflections before eventually escaping.

Answer: 354