All Euler problems
Project Euler

Investigating Multiple Reflections of a Laser Beam

A laser beam enters a white cell (an ellipse defined by 4x^2 + y^2 = 100) at the point (0, 10.1) and first hits the mirror at (1.4, -9.6). Each time the beam hits the surface, it is reflected accor...

Source sync Apr 19, 2026
Problem #0144
Level Level 05
Solved By 7,209
Languages C++, Python
Answer 354
Length 363 words
geometryalgebrasimulation

Problem Statement

This archive keeps the full statement, math, and original media on the page.

In laser physics, a "white cell" is a mirror system that acts as a delay line for the laser beam. The beam enters the cell, bounces around on the mirrors, and eventually works its way back out.

The specific white cell we will be considering is an ellipse with the equation $4x^2 + y^2 = 100$.

The section corresponding to $-0.01 \le x \le +0.01$ at the top is missing, allowing the light to enter and exit through the hole.

Problem illustration

Problem animation

The light beam in this problem starts at the point $(0.0,10.1)$ just outside the white cell, and the beam first impacts the mirror at $(1.4,-9.6)$.

Each time the laser beam hits the surface of the ellipse, it follows the usual law of reflection "angle of incidence equals angle of reflection." That is, both the incident and reflected beams make the same angle with the normal line at the point of incidence.

In the figure on the left, the red line shows the first two points of contact between the laser beam and the wall of the white cell; the blue line shows the line tangent to the ellipse at the point of incidence of the first bounce.

The slope $m$ of the tangent line at any point $(x,y)$ of the given ellipse is: $m = -4x/y$.

The normal line is perpendicular to this tangent line at the point of incidence.

The animation on the right shows the first $10$ reflections of the beam.

How many times does the beam hit the internal surface of the white cell before exiting?

Problem 144: Investigating Multiple Reflections of a Laser Beam

Mathematical Foundation

Theorem 1. For the ellipse 4x2+y2=1004x^2 + y^2 = 100, the outward unit normal at a point (x0,y0)(x_0, y_0) on the ellipse is proportional to (4x0,y0)(4x_0, y_0).

Proof. The ellipse is the level set F(x,y)=4x2+y2100=0F(x,y) = 4x^2 + y^2 - 100 = 0. The gradient F=(8x,2y)\nabla F = (8x, 2y) is perpendicular to the level curve and points outward. Simplifying by the common factor of 2:

n=(4x0,y0)\mathbf{n} = (4x_0, y_0)

This is outward-pointing because for a point on the ellipse with x0>0x_0 > 0, the xx-component is positive (pointing away from the center). \square

Theorem 2 (Reflection Formula). Given an incoming direction d\mathbf{d} and surface normal n\mathbf{n}, the reflected direction is:

d=d2dnn2n\mathbf{d}' = \mathbf{d} - 2\frac{\mathbf{d} \cdot \mathbf{n}}{|\mathbf{n}|^2}\mathbf{n}

Proof. Decompose d=d+d\mathbf{d} = \mathbf{d}_\parallel + \mathbf{d}_\perp where d=dnn2n\mathbf{d}_\parallel = \frac{\mathbf{d} \cdot \mathbf{n}}{|\mathbf{n}|^2}\mathbf{n} is the component along n\mathbf{n} and d=dd\mathbf{d}_\perp = \mathbf{d} - \mathbf{d}_\parallel is tangential. The law of reflection reverses the normal component while preserving the tangential component:

d=dd=d2d=d2dnn2n\mathbf{d}' = \mathbf{d}_\perp - \mathbf{d}_\parallel = \mathbf{d} - 2\mathbf{d}_\parallel = \mathbf{d} - 2\frac{\mathbf{d} \cdot \mathbf{n}}{|\mathbf{n}|^2}\mathbf{n}

\square

Theorem 3 (Ray—Ellipse Intersection). A ray from (x0,y0)(x_0, y_0) on the ellipse in direction (dx,dy)(d_x, d_y) intersects the ellipse again at parameter:

t=2(4x0dx+y0dy)4dx2+dy2t = -\frac{2(4x_0 d_x + y_0 d_y)}{4d_x^2 + d_y^2}

Proof. Substitute (x,y)=(x0+tdx,y0+tdy)(x, y) = (x_0 + td_x, y_0 + td_y) into 4x2+y2=1004x^2 + y^2 = 100:

4(x0+tdx)2+(y0+tdy)2=1004(x_0 + td_x)^2 + (y_0 + td_y)^2 = 100

Expanding and using 4x02+y02=1004x_0^2 + y_0^2 = 100:

t2(4dx2+dy2)+2t(4x0dx+y0dy)=0t^2(4d_x^2 + d_y^2) + 2t(4x_0 d_x + y_0 d_y) = 0 t[t(4dx2+dy2)+2(4x0dx+y0dy)]=0t\bigl[t(4d_x^2 + d_y^2) + 2(4x_0 d_x + y_0 d_y)\bigr] = 0

The solution t=0t = 0 corresponds to the starting point. The other solution is:

t=2(4x0dx+y0dy)4dx2+dy2t = -\frac{2(4x_0 d_x + y_0 d_y)}{4d_x^2 + d_y^2}

\square

Lemma 1. The parameter tt in Theorem 3 is always nonzero (unless the ray is tangent to the ellipse at the starting point), so the ray always hits the ellipse again.

Proof. The numerator 2(4x0dx+y0dy)=2(nd)2(4x_0 d_x + y_0 d_y) = 2(\mathbf{n} \cdot \mathbf{d}) vanishes only when d\mathbf{d} is tangent to the ellipse at (x0,y0)(x_0, y_0). After reflection, d\mathbf{d}' has a nonzero normal component (since the incoming ray was not tangent), so the numerator is nonzero. \square

Editorial

Simulate laser reflections inside the ellipse 4x^2 + y^2 = 100. Entry at (0, 10.1), first hit at (1.4, -9.6). Exit when |x| <= 0.01 at the top (y > 0). We repeat. We then reflect. Finally, next intersection.

Pseudocode

INPUT: Ellipse 4x^2 + y^2 = 100, entry (0, 10.1), first hit (1.4, -9.6)
OUTPUT: Number of internal reflections before exit
repeat
Reflect
Next intersection
Check exit

Complexity Analysis

  • Time: O(R)O(R) where RR is the number of reflections. Each reflection requires O(1)O(1) arithmetic operations (dot product, reflection, quadratic solve). Here R=354R = 354.
  • Space: O(1)O(1) — only the current position, direction, and normal need to be stored.

Answer

354\boxed{354}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_144/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 144: Laser reflections in ellipse 4x^2 + y^2 = 100
    // Entry: (0, 10.1) -> first hit: (1.4, -9.6)
    // Exit: |x| <= 0.01 at top (y > 0)

    double x0 = 0.0, y0 = 10.1;
    double x1 = 1.4, y1 = -9.6;

    int count = 0;

    while (true) {
        // Direction of incoming beam
        double dx = x1 - x0;
        double dy = y1 - y0;

        // Normal at (x1, y1): proportional to (4*x1, y1)
        double nx = 4.0 * x1;
        double ny = y1;

        // Reflect: d' = d - 2*(d.n)/(n.n) * n
        double dn = dx * nx + dy * ny;
        double nn = nx * nx + ny * ny;
        double rx = dx - 2.0 * dn / nn * nx;
        double ry = dy - 2.0 * dn / nn * ny;

        // Find next intersection with 4x^2 + y^2 = 100
        // Ray: (x1 + t*rx, y1 + t*ry)
        // 4(x1+t*rx)^2 + (y1+t*ry)^2 = 100
        // t * (4*rx^2 + ry^2) + 2*(4*x1*rx + y1*ry) = 0  (dividing out t=0)
        double denom = 4.0 * rx * rx + ry * ry;
        double t = -2.0 * (4.0 * x1 * rx + y1 * ry) / denom;

        double x2 = x1 + t * rx;
        double y2 = y1 + t * ry;

        count++;

        // Check exit condition
        if (fabs(x2) <= 0.01 && y2 > 0) {
            break;
        }

        x0 = x1; y0 = y1;
        x1 = x2; y1 = y2;
    }

    cout << count << endl;
    return 0;
}