All Euler problems
Project Euler

Multiplicative Order Statistics

The multiplicative order ord_n(2) is the smallest positive k such that 2^k equiv 1 (mod n). Find sum_(n=2, gcd(2,n)=1)^10000 ord_n(2).

Source sync Apr 19, 2026
Problem #0980
Level Level 29
Solved By 314
Languages C++, Python
Answer 124999683766
Length 106 words
modular_arithmeticnumber_theoryalgebra

Problem Statement

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

Starting from an empty string, we want to build a string with letters "x", "y", "z". At each step, one of the following operations is performed:

  • insert two consecutive identical letters "xx", "yy" or "zz" anywhere into the string;
  • replace one letter in the string with two consecutive letters, according to the rule: "x" "yz", "y" "zx", "z" "xy";
  • exchange two consecutive different letters in the string, e.g. "xy" "yx", "zx" "xz", etc.

A string is called neutral if it is possible to produce the string from the empty string after an even number of steps.

We define a sequence : and for .

Let . For each , a string of length is defined by translating the finite sequence via the rule: "x", "y", "z".

Let be the number of ordered pairs with such that the concatenated string is neutral.
For example, and .

Find .

Problem 980: Multiplicative Order Statistics

Mathematical Analysis

Multiplicative Order

Theorem (Euler). For gcd(a,n)=1\gcd(a, n) = 1: aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n}. Hence ordn(a)ϕ(n)\operatorname{ord}_n(a) \mid \phi(n).

Theorem. nn is a primitive root modulo pp iff ordp(n)=p1\operatorname{ord}_p(n) = p - 1.

Order of 2

The multiplicative order of 2 modulo odd nn is a fundamental quantity in number theory. It determines:

  • The period of the binary expansion of 1/n1/n.
  • The splitting behavior of the prime 2 in cyclotomic fields.

Average Order

Theorem (Hooley, Artin’s conjecture on average). The average of ordn(2)\operatorname{ord}_n(2) over odd nn is cn\sim c \cdot n for a constant cc related to Artin’s constant.

Derivation

Editorial

For each odd nn from 3 to 10000: compute 2kmodn2^k \bmod n for k=1,2,k = 1, 2, \ldots until 2k12^k \equiv 1.

Complexity Analysis

O(Nϕˉ)O(N \cdot \bar{\phi}) where ϕˉN/(2lnN)\bar{\phi} \sim N / (2\ln N), giving O(N2/logN)\sim O(N^2 / \log N).

Answer

124999683766\boxed{124999683766}

Code

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

C++ project_euler/problem_980/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long total = 0;
    for (int n = 3; n <= 10000; n += 2) {
        long long val = 2;
        for (int k = 1; k < n; k++) {
            if (val == 1) { total += k; goto next; }
            val = val * 2 % n;
        }
        next:;
    }
    cout << total << endl;
    return 0;
}