All Euler problems
Project Euler

Hexagonal Orchards

A hexagonal orchard of order n is a triangular lattice arranged in a regular hexagonal pattern with n concentric rings around a central point. A lattice point is visible from the center if no other...

Source sync Apr 19, 2026
Problem #0351
Level Level 09
Solved By 2,991
Languages C++, Python
Answer 11762187201804552
Length 358 words
number_theorygeometrybrute_force

Problem Statement

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

A hexagonal orchard of order $n$ is a triangular lattice made up of points within a regular hexagon with side $n$. The following is and example of a hexagonal orchard of order $5$:

Problem illustration

Highlighted in green are the points which are hidden from the center by a point closer to it. It can be seen that for a hexagonal orchard of order $5$, $30$ points are hidden from the center.

Let $H(n)$ be the number of points hidden from the center in a hexagonal orchard of order $n$.

We have $H(5) = 30$, $H(10) = 138$ and $H (\num{1000}) = 1177848$.

Find $H(10^8)$.

Problem 351: Hexagonal Orchards

Mathematical Foundation

We work in the standard hexagonal coordinate system with basis vectors e1=(1,0)\mathbf{e}_1 = (1,0) and e2=(1/2,3/2)\mathbf{e}_2 = (1/2, \sqrt{3}/2). Every lattice point in the hexagonal grid of order nn can be written as ae1+be2a\mathbf{e}_1 + b\mathbf{e}_2 for integers a,ba, b satisfying certain constraints. The hexagonal grid of order nn has exactly 3n(n+1)3n(n+1) lattice points excluding the center.

Theorem 1 (Visibility criterion). A lattice point P=ae1+be2P = a\mathbf{e}_1 + b\mathbf{e}_2 is visible from the origin if and only if gcd(a,b)=1\gcd(a, b) = 1.

Proof. Suppose gcd(a,b)=d>1\gcd(a,b) = d > 1. Then the point Q=(a/d)e1+(b/d)e2Q = (a/d)\mathbf{e}_1 + (b/d)\mathbf{e}_2 is a lattice point lying on the segment from the origin to PP, so PP is not visible. Conversely, if gcd(a,b)=1\gcd(a,b) = 1, then no lattice point lies strictly between the origin and PP on the line through them, since any such point would have the form (ta,tb)(ta, tb) for 0<t<10 < t < 1 rational, forcing t=k/dt = k/d with dad | a and dbd | b, contradicting gcd(a,b)=1\gcd(a,b)=1. \square

Theorem 2 (Visible point count). The number of visible lattice points in the hexagonal grid of order nn is

V(n)=6k=1nφ(k)V(n) = 6\sum_{k=1}^{n} \varphi(k)

where φ\varphi is Euler’s totient function.

Proof. By the six-fold rotational symmetry of the hexagonal lattice, it suffices to count visible points in one fundamental sector and multiply by 6. In one sector, the lattice points at distance layer kk (for 1kn1 \le k \le n) correspond to pairs (a,b)(a,b) with a+b=ka + b = k, a1a \ge 1, b0b \ge 0 (or an equivalent parameterization). A point at layer kk is visible if and only if gcd(a,b)=1\gcd(a,b) = 1. The number of integers bb with 0b<k0 \le b < k and gcd(b,k)=1\gcd(b,k) = 1 is exactly φ(k)\varphi(k). Summing over layers and multiplying by the symmetry factor gives V(n)=6k=1nφ(k)V(n) = 6\sum_{k=1}^{n}\varphi(k). \square

Corollary (Hidden point formula). The number of hidden points is

H(n)=3n(n+1)6k=1nφ(k).H(n) = 3n(n+1) - 6\sum_{k=1}^{n}\varphi(k).

Proof. Immediate from H(n)=T(n)V(n)H(n) = T(n) - V(n) where T(n)=3n(n+1)T(n) = 3n(n+1). \square

Editorial

H(n) = 3n(n+1) - 6 * sum(phi(k) for k in 1..n) We sieve Euler’s totient function up to n = 10^8. We compute Euler’s totient via linear sieve. We then accumulate prefix sum. Finally, compute hidden count.

Pseudocode

Compute Euler's totient via linear sieve
Accumulate prefix sum
Compute hidden count

Complexity Analysis

  • Time: O(nloglogn)O(n \log \log n) for the sieve of Euler’s totient function (each integer is visited once per prime factor), plus O(n)O(n) for the prefix sum. Total: O(nloglogn)O(n \log \log n).
  • Space: O(n)O(n) for the array storing φ(k)\varphi(k) for k=1,,nk = 1, \ldots, n.

Answer

11762187201804552\boxed{11762187201804552}

Code

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

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

/*
 * Problem 351: Hexagonal Orchards
 *
 * H(n) = 3n(n+1) - 6 * sum_{k=1}^{n} phi(k)
 *
 * We sieve Euler's totient function up to n = 10^8
 * and compute the prefix sum.
 */

int main() {
    const int N = 100000000; // 10^8

    // Sieve for Euler's totient function
    vector<int> phi(N + 1);
    iota(phi.begin(), phi.end(), 0); // phi[i] = i initially

    for (int i = 2; i <= N; i++) {
        if (phi[i] == i) { // i is prime
            for (int j = i; j <= N; j += i) {
                phi[j] -= phi[j] / i;
            }
        }
    }

    // Compute sum of phi[k] for k = 1..N
    long long phi_sum = 0;
    for (int k = 1; k <= N; k++) {
        phi_sum += phi[k];
    }

    // H(n) = 3n(n+1) - 6 * phi_sum
    long long n = N;
    long long total = 3LL * n * (n + 1);
    long long hidden = total - 6LL * phi_sum;

    cout << hidden << endl;

    return 0;
}