All Euler problems
Project Euler

Mobius Function Partial Sums

The Mertens function is defined as M(n) = sum_(k=1)^n mu(k), where mu is the Mobius function. Compute sum_(n=1)^(10^5) |M(n)|.

Source sync Apr 19, 2026
Problem #0974
Level Level 25
Solved By 403
Languages C++, Python
Answer 13313751171933973557517973175
Length 246 words
number_theorylinear_algebramodular_arithmetic

Problem Statement

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

A very odd number is a number which contains only odd digits and is divisible by . Further each odd digit occurs an odd number of times.

Define be the th very odd number, then and .

Find .

Problem 974: Mobius Function Partial Sums

Mathematical Foundation

Definition 1 (Mobius Function). For nZ>0n \in \mathbb{Z}_{>0}:

μ(n)={1if n=1,(1)kif n=p1p2pk with pi distinct primes,0if p2n for some prime p.\mu(n) = \begin{cases} 1 & \text{if } n = 1, \\ (-1)^k & \text{if } n = p_1 p_2 \cdots p_k \text{ with } p_i \text{ distinct primes}, \\ 0 & \text{if } p^2 \mid n \text{ for some prime } p. \end{cases}

Theorem 1 (Mobius Inversion Formula). If f(n)=dng(d)f(n) = \sum_{d \mid n} g(d) for all n1n \ge 1, then g(n)=dnμ(n/d)f(d)g(n) = \sum_{d \mid n} \mu(n/d)\, f(d).

Proof. We verify:

dnμ(n/d)f(d)=dnμ(n/d)edg(e)=eng(e)dnedμ(n/d).\sum_{d \mid n} \mu(n/d)\, f(d) = \sum_{d \mid n} \mu(n/d) \sum_{e \mid d} g(e) = \sum_{e \mid n} g(e) \sum_{\substack{d \mid n \\ e \mid d}} \mu(n/d).

Setting d=ed = e\ell, the inner sum becomes (n/e)μ(n/(e))=m(n/e)μ(m)=[n/e=1]=[n=e]\sum_{\ell \mid (n/e)} \mu(n/(e\ell))= \sum_{m \mid (n/e)} \mu(m) = [n/e = 1] = [n = e], using the fundamental identity dkμ(d)=[k=1]\sum_{d \mid k} \mu(d) = [k=1]. Hence the double sum equals g(n)g(n). \square

Theorem 2 (Fundamental Identity of μ\mu). For all n1n \ge 1, dnμ(d)=[n=1]\sum_{d \mid n} \mu(d) = [n = 1].

Proof. For n=1n = 1, the sum is μ(1)=1\mu(1) = 1. For n>1n > 1, write n=p1a1prarn = p_1^{a_1} \cdots p_r^{a_r}. Then dnμ(d)=S{p1,,pr}(1)S=(11)r=0\sum_{d \mid n} \mu(d) = \sum_{S \subseteq \{p_1,\ldots,p_r\}} (-1)^{|S|} = (1-1)^r = 0, since only squarefree divisors contribute. \square

Theorem 3 (Mertens Function and the Riemann Hypothesis). The Riemann Hypothesis is equivalent to the assertion that M(n)=O(n1/2+ε)M(n) = O(n^{1/2+\varepsilon}) for every ε>0\varepsilon > 0.

Proof. (Sketch.) The Dirichlet series n=1μ(n)/ns=1/ζ(s)\sum_{n=1}^\infty \mu(n)/n^s = 1/\zeta(s) converges for (s)>1\Re(s) > 1. By Perron’s formula, M(x)=12πicic+ixssζ(s)dsM(x) = \frac{1}{2\pi i}\int_{c-i\infty}^{c+i\infty} \frac{x^s}{s\,\zeta(s)}\,ds. The poles of 1/ζ(s)1/\zeta(s) correspond to zeros of ζ(s)\zeta(s). If all nontrivial zeros satisfy (ρ)=1/2\Re(\rho) = 1/2, the residue contributions give M(x)=O(x1/2+ε)M(x) = O(x^{1/2+\varepsilon}). Conversely, if M(x)=O(x1/2+ε)M(x) = O(x^{1/2+\varepsilon}) for all ε>0\varepsilon > 0, then 1/ζ(s)1/\zeta(s) converges for (s)>1/2\Re(s) > 1/2, implying ζ(s)0\zeta(s) \neq 0 there. \square

Lemma 1 (Computing μ\mu via Sieve). The Mobius function can be computed for all nNn \le N in O(NloglogN)O(N \log\log N) time using a modified Sieve of Eratosthenes that tracks the smallest prime factor and squarefree status.

Proof. Initialize μ(1)=1\mu(1) = 1. For each prime pp found by the sieve, for each multiple m=p,2p,3p,m = p, 2p, 3p, \ldots: if p2mp^2 \mid m, set μ(m)=0\mu(m) = 0; otherwise, multiply μ(m)\mu(m) by 1-1. This correctly computes μ\mu since μ\mu is multiplicative and μ(pa)=1\mu(p^a) = -1 if a=1a = 1, 00 if a2a \ge 2. The sieve visits each multiple of each prime, totaling O(pNN/p)=O(NloglogN)O(\sum_{p \le N} N/p) = O(N \log\log N). \square

Editorial

Compute the sum of |M(n)| for n = 1 to 10^5, where M(n) = sum_{k=1}^{n} mu(k) is the Mertens function. We compute mu via sieve. Finally, compute prefix sums and accumulate |M(n)|.

Pseudocode

Compute mu via sieve
Compute prefix sums and accumulate |M(n)|

Complexity Analysis

  • Time: O(NloglogN)O(N \log\log N) for the Mobius sieve, plus O(N)O(N) for the accumulation loop. Total: O(NloglogN)O(N \log\log N).
  • Space: O(N)O(N) for the arrays μ\mu and is_composite.

Answer

13313751171933973557517973175\boxed{13313751171933973557517973175}

Code

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

C++ project_euler/problem_974/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 100000;
    vector<int> mu(N+1,0); mu[1]=1;
    vector<bool> is_p(N+1,true); vector<int> primes;
    for(int i=2;i<=N;i++){
        if(is_p[i]){primes.push_back(i);mu[i]=-1;}
        for(int p:primes){
            if((long long)i*p>N)break;
            is_p[i*p]=false;
            if(i%p==0){mu[i*p]=0;break;}
            else mu[i*p]=-mu[i];
        }
    }
    long long M=0, ans=0;
    for(int n=1;n<=N;n++){M+=mu[n]; ans+=abs(M);}
    cout<<ans<<endl;
    return 0;
}