All Euler problems
Project Euler

Laserbeam

Three mirrors are arranged in the shape of an equilateral triangle, with their reflective surfaces pointing inwards. There is an infinitesimal gap at each vertex through which a laser beam may pass...

Source sync May 21, 2026
Problem #0202
Level Level 09
Solved By 2,977
Languages C++, Python
Answer 1209002624
Length 517 words
modular_arithmeticnumber_theorygraph

Problem Statement

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

Three mirrors are arranged in the shape of an equilateral triangle, with their reflective surfaces pointing inwards. There is an infinitesimal gap at each vertex of the triangle through which a laser beam may pass.

Label the vertices \(A\), \(B\) and \(C\). There are \(2\) ways in which a laser beam may enter vertex \(C\), bounce off \(11\) surfaces, then exit through the same vertex: one way is shown below; the other is the reverse of that.

PIC

There are \(80840\) ways in which a laser beam may enter vertex \(C\), bounce off \(1000001\) surfaces, then exit through the same vertex.

In how many ways can a laser beam enter at vertex \(C\), bounce off \(12017639147\) surfaces, then exit through the same vertex?

Problem 202: Laserbeam

Mathematical Development

Theorem (Unfolding Principle). A laser beam making RR reflections inside an equilateral triangle corresponds, via the standard unfolding technique, to a straight line in the triangular tessellation of the plane. The line starts at the origin and terminates at a lattice point (a,b)(a, b) in the oblique coordinate system, where a+b=na + b = n with n=(R+3)/2n = (R + 3)/2.

Proof. Each reflection off a mirror wall is equivalent to reflecting the triangle across that wall and continuing the beam in a straight line. After RR reflections, the beam has crossed RR triangle edges. The beam enters at vertex CC and must exit at a vertex, crossing RR edges total. In the oblique coordinate system where the three families of parallel lines in the tessellation are indexed by coordinates (a,b)(a, b) with a+b=na + b = n, careful counting of crossings gives n=(R+3)/2n = (R + 3)/2. For R=12017639147R = 12017639147, we get n=6008819575n = 6008819575. □\square

Theorem (Exit Vertex Classification). The beam exits through vertex CC if and only if b≢0(mod3)b \not\equiv 0 \pmod{3} and a≢0(mod3)a \not\equiv 0 \pmod{3}, which (given a+b=na + b = n with n≡1(mod3)n \equiv 1 \pmod{3}) is equivalent to b≡2(mod3)b \equiv 2 \pmod{3}.

Proof. In the triangular tessellation, a lattice point (a,b)(a, b) corresponds to a copy of vertex AA, BB, or CC depending on the residues of aa and bb modulo 3. Since n=a+b≡1(mod3)n = a + b \equiv 1 \pmod{3} and we require the endpoint to be a copy of CC, the condition reduces to b≡2(mod3)b \equiv 2 \pmod{3}. □\square

Theorem (No Intermediate Vertex Condition). The beam passes through no intermediate vertex, and thus does not exit prematurely, if and only if gcd⁡(a,b)=1\gcd(a, b) = 1, equivalently gcd⁡(b,n)=1\gcd(b, n) = 1.

Proof. If d=gcd⁡(a,b)>1d = \gcd(a, b) > 1, then (a/d,b/d)(a/d, b/d) is an intermediate lattice point on the line segment from (0,0)(0,0) to (a,b)(a,b), so the beam would reach a vertex earlier. Conversely, if gcd⁡(a,b)=1\gcd(a,b) = 1, no intermediate lattice point lies on the segment. Since a=n−ba = n - b, we have gcd⁡(a,b)=gcd⁡(n−b,b)=gcd⁡(n,b)\gcd(a,b) = \gcd(n-b, b) = \gcd(n, b). □\square

Theorem (Counting via Mobius Inversion). The number of valid beam paths is

Count=∑d∣nμ(d)⋅C(d),\text{Count} = \sum_{d \mid n} \mu(d) \cdot C(d),

where C(d)=#{b∈[1,n−1]:d∣b,  b≡2(mod3)}C(d) = \#\{b \in [1, n-1] : d \mid b,\; b \equiv 2 \pmod{3}\} and μ\mu is the Mobius function.

Proof. We seek

#{b∈[1,n−1]:gcd⁡(b,n)=1,  b≡2(mod3)}.\#\{b \in [1, n-1] : \gcd(b, n) = 1,\; b \equiv 2 \pmod{3}\}.

By Mobius inversion, ∑d∣gcd⁡(b,n)μ(d)=[gcd⁡(b,n)=1]\sum_{d \mid \gcd(b,n)} \mu(d) = [\gcd(b,n) = 1]. Exchanging the order of summation gives

Count=∑d∣nμ(d)∑b=1d∣b,b≡2(mod3)n−11=∑d∣nμ(d)⋅C(d).\text{Count} = \sum_{d \mid n} \mu(d) \sum_{\substack{b=1 \\ d \mid b,\; b \equiv 2 \pmod{3}}}^{n-1} 1 = \sum_{d \mid n} \mu(d) \cdot C(d).

For each squarefree divisor dd of nn with gcd⁡(d,3)=1\gcd(d, 3) = 1, writing b=dkb = dk gives k∈[1,n/d−1]k \in [1, n/d - 1] with dk≡2(mod3)dk \equiv 2 \pmod{3}, so the inner count is a simple arithmetic progression count. □\square

Lemma (Factorization). n=6008819575=52×11×17×23×29×41×47.n = 6008819575 = 5^2 \times 11 \times 17 \times 23 \times 29 \times 41 \times 47. Since 3∤n3 \nmid n, all 27=1282^7 = 128 squarefree divisors built from the distinct prime factors may contribute.

Proof. Verified by trial division. Since 52∣n5^2 \mid n, the Mobius function vanishes on divisors containing 525^2, so it is enough to enumerate squarefree divisors built from {5,11,17,23,29,41,47}\{5, 11, 17, 23, 29, 41, 47\}. □\square

Editorial

The geometric part is handled by unfolding the reflections. Instead of following a bouncing beam inside one triangle, we follow a straight segment in the triangular tiling. For the given reflection count, every admissible beam corresponds to an endpoint on the line a+b=na + b = n, where n=(R+3)/2=6008819575n = (R + 3)/2 = 6008819575.

Two arithmetic conditions remain. The endpoint must be a copy of vertex CC, which becomes the congruence b≡2(mod3)b \equiv 2 \pmod{3}, and the segment must not hit an earlier lattice vertex, which becomes gcd⁡(b,n)=1\gcd(b, n) = 1. That leaves a clean number-theoretic count: integers in one residue class modulo 3 that are also coprime to nn. Mobius inversion removes the coprimality condition by inclusion-exclusion over the squarefree divisors of nn.

Pseudocode

Set R = 12017639147 and n = (R + 3) / 2.
Factor n and keep its distinct prime divisors p_1, ..., p_k.

answer = 0
For each bitmask from 0 to 2^k - 1:
    Let d be the product of the primes selected by the bitmask.
    Let mu be +1 if the number of selected primes is even, else -1.

    We need multiples b = d * t with 1 <= t <= n / d - 1
    and d * t congruent to 2 modulo 3.

    Solve the congruence for t modulo 3.
    Count how many t in the interval satisfy that residue class.
    Add mu times that count to answer.

Return answer

Complexity Analysis

  • Time: O(n)O(\sqrt{n}) for factorization, plus O(2k)O(2^k) for the Mobius sum where k=7k = 7 is the number of distinct prime factors.
  • Space: O(k)O(k) to store the distinct prime factors.

Answer

1209002624\boxed{1209002624}

Code

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

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

int main() {
    long long R = 12017639147LL;
    long long n = (R + 3) / 2;

    vector<long long> primes;
    long long tmp = n;
    for (long long p = 2; p * p <= tmp; p++) {
        if (tmp % p == 0) {
            primes.push_back(p);
            while (tmp % p == 0) {
                tmp /= p;
            }
        }
    }
    if (tmp > 1) {
        primes.push_back(tmp);
    }

    long long answer = 0;
    for (int mask = 0; mask < (1 << static_cast<int>(primes.size())); mask++) {
        long long d = 1;
        int bits = 0;
        for (int i = 0; i < static_cast<int>(primes.size()); i++) {
            if (mask & (1 << i)) {
                d *= primes[i];
                bits++;
            }
        }

        long long mu = (bits % 2 == 0) ? 1 : -1;
        long long nd = n / d;
        long long count = 0;

        if (d % 3 != 0) {
            long long inverseMod3 = (d % 3 == 1) ? 1 : 2;
            long long residue = (2 * inverseMod3) % 3;
            long long upper = nd - 1;

            if (residue == 0) {
                if (upper >= 3) {
                    count = (upper - 3) / 3 + 1;
                }
            } else {
                if (upper >= residue) {
                    count = (upper - residue) / 3 + 1;
                }
            }
        }

        answer += mu * count;
    }

    cout << answer << '\n';
    return 0;
}