All Euler problems
Project Euler

Regular Star Polygons

A regular star polygon {n/k} is formed by connecting every k -th point out of n equally spaced points on a circle of radius r. The star polygon is valid when 1 < k < n/2 and gcd(n, k) = 1. Compute...

Source sync Apr 19, 2026
Problem #0841
Level Level 35
Solved By 214
Languages C++, Python
Answer 381.7860132854
Length 323 words
geometrynumber_theorymodular_arithmetic

Problem Statement

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

The regular star polygon $\{p/q\}$, for coprime integers $p,q$ with $p < 2q < 0$, is a polygon formed from $p$ edges of equal length and equal internal angles, such that tracing the complete polygon wraps $q$ times around the centre. For example, $\{8/3\}$ is illustrated below:

Problem illustration

The edges of a regular star polygon intersect one another, dividing the interior into several regions. Define the alternating shading of a regular star polygon to be a selection of such regions to shade, such that every piece of every edge has a shaded region on one side and an unshaded region on the other, with the exterior of the polygon unshaded. For example, the above image shows the alternating shading (in green) of $\{8/3\}$.

Let $A(p, q)$ be the area of the alternating shading of $\{p/q\}$, assuming that its inradius is $1$. (The inradius of a regular polygon, star or otherwise, is the distance from its centre to the midpoint of any of its edges.) For example, in the diagram above, it can be shown that central shaded octagon has area $8(\sqrt{2}-1)$ and each point's shaded kite has area $2(\sqrt{2}-1)$, giving $A(8,3) = 24(\sqrt{2}-1) \approx 9.9411254970$.

You are also given that $A(130021, 50008)\approx 10.9210371479$, rounded to $10$ digits after the decimal point.

Find $\displaystyle{\sum}_{n=3}^{34} A(F_{n+1},F_{n-1})$, where $F_j$ is the Fibonacci sequence with $F_1=F_2=1$ (so $A(F_{5+1},F_{5-1}) = A(8,3)$). Give your answer rounded to $10$ digits after the decimal point.

Problem 841: Regular Star Polygons

Mathematical Foundation

Theorem 1 (Area of a Regular Star Polygon). The area of the regular star polygon {n/k}\{n/k\} inscribed in a circle of radius rr is

A({n/k})=nr22sin ⁣(2πkn).A(\{n/k\}) = \frac{n r^2}{2} \sin\!\left(\frac{2\pi k}{n}\right).

Proof. Label the nn equally spaced points on the circle as P0,P1,,Pn1P_0, P_1, \ldots, P_{n-1} with Pj=r(cos(2πj/n),sin(2πj/n))P_j = r(\cos(2\pi j/n),\, \sin(2\pi j/n)). The star polygon {n/k}\{n/k\} connects PjP_j to Pj+kmodnP_{j+k \bmod n} for each jj. Since gcd(n,k)=1\gcd(n,k)=1, this produces a single closed polygon visiting all nn vertices.

Decompose the polygon into nn triangles OPjPj+k\triangle O P_j P_{j+k} where OO is the center. Each triangle has two sides of length rr (the radii OPjOP_j and OPj+kOP_{j+k}) with included angle PjOPj+k=2πk/n\angle P_j O P_{j+k} = 2\pi k/n. By the formula for the area of a triangle with two sides and included angle:

Area(OPjPj+k)=12r2sin ⁣(2πkn).\text{Area}(\triangle O P_j P_{j+k}) = \frac{1}{2} r^2 \sin\!\left(\frac{2\pi k}{n}\right).

Summing over all nn triangles yields

A({n/k})=nr22sin ⁣(2πkn)=nr22sin ⁣(2πkn).A(\{n/k\}) = n \cdot \frac{r^2}{2}\sin\!\left(\frac{2\pi k}{n}\right) = \frac{n r^2}{2}\sin\!\left(\frac{2\pi k}{n}\right). \quad \square

Lemma 1 (Count of Valid Star Polygons). For n5n \ge 5, the number of valid values of kk (i.e., integers kk with 1<k<n/21 < k < n/2 and gcd(n,k)=1\gcd(n,k)=1) is

φ(n)21.\frac{\varphi(n)}{2} - 1.

Proof. The integers kk with 1kn11 \le k \le n-1 and gcd(n,k)=1\gcd(n,k)=1 number φ(n)\varphi(n) by definition of Euler’s totient. These coprime residues pair off as (k,nk)(k, n-k) since gcd(n,k)=gcd(n,nk)\gcd(n,k)=\gcd(n,n-k) and knkk \ne n-k (because n/2n/2 is either non-integer or not coprime to nn for n>2n > 2). Each pair has exactly one member in {1,,(n1)/2}\{1, \ldots, \lfloor(n-1)/2\rfloor\}. Thus there are φ(n)/2\varphi(n)/2 values of kk with 1k<n/21 \le k < n/2 and gcd(n,k)=1\gcd(n,k)=1. Excluding k=1k=1 (the convex regular polygon, not a star polygon) gives φ(n)/21\varphi(n)/2 - 1. \square

Lemma 2 (Ramanujan Sum Connection). Define the Ramanujan sum cn(m)=k=1gcd(k,n)=1ne2πikm/nc_n(m) = \sum_{\substack{k=1 \\ \gcd(k,n)=1}}^{n} e^{2\pi i k m/n}. Then cn(1)=μ(n)c_n(1) = \mu(n), the Mobius function. Consequently,

1kn1gcd(k,n)=1sin ⁣(2πkn)=Im(cn(1))=Im(μ(n))=0,\sum_{\substack{1 \le k \le n-1 \\ \gcd(k,n)=1}} \sin\!\left(\frac{2\pi k}{n}\right) = \operatorname{Im}(c_n(1)) = \operatorname{Im}(\mu(n)) = 0,

since μ(n){1,0,1}R\mu(n) \in \{-1, 0, 1\} \subset \mathbb{R}.

Proof. The identity cn(1)=μ(n)c_n(1) = \mu(n) is classical (see Hardy & Wright, Ch. 16). Since μ(n)\mu(n) is real, its imaginary part vanishes. The left-hand side is Im ⁣(gcd(k,n)=1e2πik/n)=Im(cn(1))\operatorname{Im}\!\left(\sum_{\gcd(k,n)=1} e^{2\pi i k/n}\right) = \operatorname{Im}(c_n(1)). \square

Editorial

Optimized version* (sieve-based). We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

    total = 0.0
    For n from 5 to N:
        For k from 2 to floor((n-1)/2):
            If gcd(n, k) == 1 then
                total += (n / 2.0) * sin(2 * pi * k / n)
    Return total

Optimized version (sieve-based):


    total = 0.0
    Precompute smallest prime factor via sieve for gcd speedup
    spf = sieve_smallest_prime_factor(N)
    For n from 5 to N:
        For k from 2 to floor((n-1)/2):
            if gcd(n, k) == 1: # O(log n) via Euclidean algorithm
                total += (n / 2.0) * sin(2 * pi * k / n)
    Return total

Complexity Analysis

  • Time: O ⁣(n=5Nφ(n)2)=O(N2)O\!\left(\sum_{n=5}^{N} \frac{\varphi(n)}{2}\right) = O(N^2), since n=1Nφ(n)=Θ(N2)\sum_{n=1}^{N}\varphi(n) = \Theta(N^2). Each inner iteration performs one gcd\gcd (O(logn)O(\log n)) and one sin\sin evaluation (O(1)O(1) with standard libraries), so total time is O(N2logN)O(N^2 \log N) in the worst case, or O(N2)O(N^2) treating gcd\gcd as O(1)O(1) amortized.
  • Space: O(1)O(1) auxiliary (or O(N)O(N) if a sieve is used for gcd\gcd acceleration).

Answer

381.7860132854\boxed{381.7860132854}

Code

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

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

/*
 * Problem 841: Regular Star Polygons
 *
 * Sum of areas of all valid star polygons {n/k} inscribed in unit circle.
 * Area({n/k}) = (n/2) * sin(2*pi*k/n)
 * Valid when gcd(n,k) = 1 and 1 < k < n/2.
 */

int main() {
    int N = 10000;
    double total = 0.0;
    const double PI = acos(-1.0);

    for (int n = 3; n <= N; n++) {
        for (int k = 2; k < n / 2 + 1; k++) {
            if (n % 2 == 0 && k == n / 2) continue;
            if (__gcd(n, k) == 1) {
                total += (n / 2.0) * sin(2.0 * PI * k / n);
            }
        }
    }

    // Verify small case: {5/2} pentagram
    double pent = 2.5 * sin(2.0 * PI * 2 / 5);
    assert(abs(pent - 1.46946) < 0.001);

    printf("%.3f\n", total);
    return 0;
}