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...
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$:

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 and . Every lattice point in the hexagonal grid of order can be written as for integers satisfying certain constraints. The hexagonal grid of order has exactly lattice points excluding the center.
Theorem 1 (Visibility criterion). A lattice point is visible from the origin if and only if .
Proof. Suppose . Then the point is a lattice point lying on the segment from the origin to , so is not visible. Conversely, if , then no lattice point lies strictly between the origin and on the line through them, since any such point would have the form for rational, forcing with and , contradicting .
Theorem 2 (Visible point count). The number of visible lattice points in the hexagonal grid of order is
where 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 (for ) correspond to pairs with , , (or an equivalent parameterization). A point at layer is visible if and only if . The number of integers with and is exactly . Summing over layers and multiplying by the symmetry factor gives .
Corollary (Hidden point formula). The number of hidden points is
Proof. Immediate from where .
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: for the sieve of Euler’s totient function (each integer is visited once per prime factor), plus for the prefix sum. Total: .
- Space: for the array storing for .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 351: Hexagonal Orchards
H(n) = 3*n*(n+1) - 6 * sum(phi(k) for k in 1..n)
We sieve Euler's totient function up to n = 10^8.
"""
def solve():
N = 100000000 # 10^8
# Sieve for Euler's totient function
phi = list(range(N + 1))
for i in range(2, N + 1):
if phi[i] == i: # i is prime
for j in range(i, N + 1, i):
phi[j] -= phi[j] // i
# Compute sum of phi[k] for k = 1..N
phi_sum = sum(phi[1:])
# H(n) = 3*n*(n+1) - 6 * phi_sum
total = 3 * N * (N + 1)
hidden = total - 6 * phi_sum
print(hidden)
if __name__ == "__main__":
solve()