All Euler problems
Project Euler

Josephus Problem Variant

In the Josephus problem, n people stand in a circle and every k -th person is eliminated. Let J(n,k) be the 1-indexed position of the last survivor. Find sum_(n=1)^10000 J(n, 3).

Source sync Apr 19, 2026
Problem #0979
Level Level 35
Solved By 211
Languages C++, Python
Answer 189306828278449
Length 128 words
geometrysequencemodular_arithmetic

Problem Statement

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

The hyperbolic plane, represented by the open unit disc, can be tiled by heptagons. Every tile is a hyperbolic heptagon (i.e. it has seven edges which are segments of geodesics in the hyperbolic plane) and every vertex is shared by three tiles.
Please refer to Problem 972 for some of the definitions.

The diagram below shows an illustration of this tiling.

0979_heptagons_frog.png

Now, a hyperbolic frog starts from one of the heptagons, as shown in the diagram. At each step, it can jump to any one of the seven adjacent tiles.

Define to be the number of paths the frog can trace so that after steps it lands back at the starting tile.
You are given .

Find .

Problem 979: Josephus Problem Variant

Mathematical Analysis

Josephus Recurrence

Theorem. The 0-indexed Josephus number satisfies:

J0(1)=0,J0(n,k)=(J0(n1,k)+k)modnJ_0(1) = 0, \qquad J_0(n, k) = (J_0(n-1, k) + k) \bmod n

The 1-indexed version is J(n,k)=J0(n,k)+1J(n, k) = J_0(n, k) + 1.

Proof. After the first elimination (person at position k1k-1), the remaining n1n-1 people form a circle starting from position kk. Relabeling gives the recurrence. \square

Properties for k=3k = 3

Proposition. J(n,3)J(n, 3) cycles through residues modulo 3 with complex patterns. No simple closed form exists for general nn.

Asymptotic Behavior

For fixed kk and large nn: J(n,k)kk1(J(n1,k))+correctionJ(n, k) \approx \frac{k}{k-1}(J(n-1,k)) + \text{correction}. The average value of J(n,k)J(n,k) is approximately n(k1)/kn \cdot (k-1)/k.

Derivation

Editorial

Compute J(n,3)J(n, 3) for each nn from 1 to 10000 using the iterative recurrence (building up from J(1)=0J(1) = 0).

Complexity Analysis

O(N)O(N) per value (iterating recurrence from 1 to nn), or O(N)O(N) total using incremental computation.

Answer

189306828278449\boxed{189306828278449}

Code

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

C++ project_euler/problem_979/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long total = 0;
    for (int n = 1; n <= 10000; n++) {
        int j = 0;
        for (int i = 2; i <= n; i++) j = (j + 3) % i;
        total += j + 1;
    }
    cout << total << endl;
    return 0;
}