All Euler problems
Project Euler

Counting Squarefree Numbers in Range

A positive integer is squarefree if no perfect square other than 1 divides it. Let Q(n) be the count of squarefree numbers up to n. Find Q(10^12).

Source sync Apr 19, 2026
Problem #0948
Level Level 26
Solved By 385
Languages C++, Python
Answer 1033654680825334184
Length 133 words
linear_algebramodular_arithmeticbrute_force

Problem Statement

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

Left and Right play a game with a word consisting of L’s and R’s, alternating turns. On Left’s turn, Left can remove any positive number of letters, but not all the letters, from the left side of the word. Right does the same on Right’s turn except that Right removes letters from the right side. The game continues until only one letter remains: if it is an ’L’ then Left wins; if it is an ’R’ then Right wins.

Let \(F(n)\) be the number of words of length \(n\) where the player moving first, whether it’s Left or Right, will win the game if both play optimally.

You are given \(F(3)=4\) and \(F(8)=181\).

Find \(F(60)\).

Problem 948: Counting Squarefree Numbers in Range

Mathematical Analysis

Squarefree Numbers

Definition. nn is squarefree if μ(n)0\mu(n) \neq 0, equivalently, p2np^2 \nmid n for all primes pp.

Density

Theorem. Q(n)=6nπ2+O(n)Q(n) = \frac{6n}{\pi^2} + O(\sqrt{n}).

More precisely: Q(n)=nζ(2)+O(n)=6nπ2+O(n)Q(n) = \frac{n}{\zeta(2)} + O(\sqrt{n}) = \frac{6n}{\pi^2} + O(\sqrt{n}).

Inclusion-Exclusion Formula

Theorem. Q(n)=d=1nμ(d)n/d2Q(n) = \sum_{d=1}^{\lfloor\sqrt{n}\rfloor} \mu(d) \lfloor n/d^2 \rfloor.

Proof. A number mnm \leq n is squarefree iff it is not divisible by any d2d^2 for d2d \geq 2. By Mobius inversion:

Q(n)=m=1nμ(m)0...Actually: Q(n)=d=1μ(d)n/d2Q(n) = \sum_{m=1}^{n} |\mu(m)|^0... \text{Actually: } Q(n) = \sum_{d=1}^{\infty} \mu(d) \lfloor n/d^2 \rfloor

since d2mμ(d)=μ(m)\sum_{d^2 | m} \mu(d) = |\mu(m)| (the indicator of squarefreeness). Truncate at d=nd = \lfloor\sqrt{n}\rfloor. \square

Algorithm for Large nn

  1. Sieve μ(d)\mu(d) for dn=106d \leq \sqrt{n} = 10^6.
  2. Compute μ(d)n/d2\sum \mu(d) \lfloor n/d^2 \rfloor.

Concrete Values

nnQ(n)Q(n)6n/π26n/\pi^2
1076.08
1006160.8
1000608607.9

Proof of Correctness

  1. Mobius inversion: d2mμ(d)=[m squarefree]\sum_{d^2|m} \mu(d) = [m \text{ squarefree}].
  2. Sieve: Standard Mobius sieve.
  3. Truncation: μ(d)=0\mu(d) = 0 for d>nd > \sqrt{n} contributes n/d2=0\lfloor n/d^2 \rfloor = 0.

Complexity Analysis

  • Sieve: O(nloglogn)O(\sqrt{n} \log \log \sqrt{n}).
  • Sum: O(n)O(\sqrt{n}).
  • Total: O(n)O(\sqrt{n}) time.

Answer

1033654680825334184\boxed{1033654680825334184}

Code

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

C++ project_euler/problem_948/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long N=1000000000000LL;
    int sqN=(int)sqrt((double)N);
    while((long long)(sqN+1)*(sqN+1)<=N) sqN++;
    vector<int> mu(sqN+1,0);
    mu[1]=1;
    vector<bool> is_p(sqN+1,true);
    vector<int> primes;
    for(int i=2;i<=sqN;i++){
        if(is_p[i]){primes.push_back(i);mu[i]=-1;}
        for(int p:primes){
            if((long long)i*p>sqN) break;
            is_p[i*p]=false;
            if(i%p==0){mu[i*p]=0;break;}
            else mu[i*p]=-mu[i];
        }
    }
    long long ans=0;
    for(int k=1;k<=sqN;k++) if(mu[k]) ans+=mu[k]*(N/((long long)k*k));
    cout<<ans<<endl;
    return 0;
}