All Euler problems
Project Euler

Squarefree Numbers

How many squarefree numbers are there below 2^50? A number is squarefree if it is not divisible by any perfect square other than 1.

Source sync May 21, 2026
Problem #0193
Level Level 07
Solved By 3,987
Languages C++, Python
Answer 684465067343069
Length 304 words
number_theorylinear_algebrabrute_force

Problem Statement

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

A positive integer \(n\) is called squarefree, if no square of a prime divides \(n\), thus \(1, 2, 3, 5, 6, 7, 10, 11\) are squarefree, but not \(4, 8, 9, 12\).

How many squarefree numbers are there below \(2^{50}\)?

Problem 193: Squarefree Numbers

Mathematical Development

Theorem 1. (Squarefree Indicator Identity.) For all positive integers nn,

∣μ(n)∣2=∑d2∣nμ(d),|\mu(n)|^2 = \sum_{d^2 \mid n} \mu(d),

where μ\mu is the Mobius function. In particular, ∣μ(n)∣2=1|\mu(n)|^2 = 1 if nn is squarefree and 00 otherwise.

Proof. Both sides are multiplicative functions of nn, so it suffices to verify equality at prime powers n=pkn = p^k.

  • For k=1k = 1: ∣μ(p)∣2=1|\mu(p)|^2 = 1. The divisors dd with d2∣pd^2 \mid p are only d=1d = 1, so ∑d2∣pμ(d)=μ(1)=1\sum_{d^2 \mid p} \mu(d) = \mu(1) = 1. □case\square_{\text{case}}
  • For k≥2k \geq 2: ∣μ(pk)∣2=0|\mu(p^k)|^2 = 0. The divisors dd with d2∣pkd^2 \mid p^k are d∈{1,p,…,p⌊k/2⌋}d \in \{1, p, \ldots, p^{\lfloor k/2 \rfloor}\}. For k≥2k \geq 2, at least d=1d = 1 and d=pd = p appear, giving μ(1)+μ(p)+⋯\mu(1) + \mu(p) + \cdots. Since μ(pj)=0\mu(p^j) = 0 for j≥2j \geq 2, this equals μ(1)+μ(p)=1+(−1)=0\mu(1) + \mu(p) = 1 + (-1) = 0. □case\square_{\text{case}}

By multiplicativity, the identity holds for all nn. □\square

Theorem 2. (Squarefree Counting Formula.) The number of squarefree integers not exceeding NN is

Q(N)=∑k=1⌊N⌋μ(k)⌊Nk2⌋.Q(N) = \sum_{k=1}^{\lfloor \sqrt{N} \rfloor} \mu(k) \left\lfloor \frac{N}{k^2} \right\rfloor.

Proof. By Theorem 1:

Q(N)=∑n=1N∣μ(n)∣2=∑n=1N∑d2∣nμ(d)=∑d=1⌊N⌋μ(d)∑n≤Nd2∣n1=∑d=1⌊N⌋μ(d)⌊Nd2⌋.Q(N) = \sum_{n=1}^{N} |\mu(n)|^2 = \sum_{n=1}^{N} \sum_{d^2 \mid n} \mu(d) = \sum_{d=1}^{\lfloor\sqrt{N}\rfloor} \mu(d) \sum_{\substack{n \leq N \\ d^2 \mid n}} 1 = \sum_{d=1}^{\lfloor\sqrt{N}\rfloor} \mu(d) \left\lfloor \frac{N}{d^2} \right\rfloor.

The exchange of summation is justified since d2∣nd^2 \mid n and n≤Nn \leq N imply d≤Nd \leq \sqrt{N}. □\square

Lemma 1. (Mobius Sieve.) The values μ(k)\mu(k) for 1≤k≤M1 \leq k \leq M can be computed in O(Mlog⁡log⁡M)O(M \log \log M) time and O(M)O(M) space using a modified Eratosthenes sieve.

Proof. Initialize μ(k)=1\mu(k) = 1 for all kk. For each prime p≤Mp \leq M: multiply μ(k)\mu(k) by −1-1 for all multiples kk of pp; set μ(k)=0\mu(k) = 0 for all multiples kk of p2p^2. Since each composite is processed once per prime factor, the total work is ∑p≤MM/p=O(Mlog⁡log⁡M)\sum_{p \leq M} M/p = O(M \log \log M) by Mertens’ theorem. □\square

Editorial

The direct definition of squarefree numbers is awkward for counting, but the Mobius inversion formula turns the indicator into a divisor sum over squares. After that transformation, the problem stops being about testing individual integers and becomes a single summation

Q(N)=∑k≤Nμ(k)⌊Nk2⌋.Q(N) = \sum_{k \le \sqrt{N}} \mu(k)\left\lfloor \frac{N}{k^2}\right\rfloor.

So the whole task reduces to computing the Mobius function up to sqrt(2^50) = 2^25.

That is exactly what the modified sieve does. Each prime flips the sign of mu on its multiples, and each square of a prime wipes the value back to zero. Once the sieve is finished, one linear pass through k = 1 .. 2^25 evaluates the formula above and accumulates the squarefree count.

Pseudocode

Let N = 2^50 - 1 and M = floor(sqrt(N)).
Initialize mu(1..M) for a Mobius sieve.

For each prime p up to M:
    flip the sign of mu on every multiple of p;
    set mu to 0 on every multiple of p^2.

Set the answer to 0.
For each k from 1 to M:
    add mu(k) * floor(N / k^2) to the answer.

Return the answer.

Complexity Analysis

  • Time: O(Nlog⁡log⁡N)O(\sqrt{N} \log \log \sqrt{N}) for the Mobius sieve, plus O(N)O(\sqrt{N}) for the summation. Total: O(Nlog⁡log⁡N)O(\sqrt{N} \log \log \sqrt{N}).
  • Space: O(N)O(\sqrt{N}) for storing the μ\mu values. For N=250N = 2^{50}, N=225≈3.4×107\sqrt{N} = 2^{25} \approx 3.4 \times 10^7.

Answer

684465067343069\boxed{684465067343069}

Code

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

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

int main() {
    // Count squarefree numbers below 2^50
    // Q(N) = sum_{k=1}^{sqrt(N)} mu(k) * floor(N/k^2)
    const long long N = (1LL << 50) - 1;
    const int SQ = 1 << 25; // sqrt(2^50) = 2^25

    // Sieve Mobius function
    vector<int> mu(SQ + 1, 1);
    vector<bool> is_prime(SQ + 1, true);
    vector<int> primes;

    // We'll use a sieve that tracks the smallest prime factor
    // and whether a number is divisible by p^2 for any prime p
    // Simple approach: for each prime p, multiply mu by -1 for multiples of p,
    // set mu=0 for multiples of p^2

    for (int i = 2; i <= SQ; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
            // i is prime
            for (long long j = i; j <= SQ; j += i) {
                is_prime[j] = (j == i);
                mu[j] *= -1;
            }
            long long i2 = (long long)i * i;
            for (long long j = i2; j <= SQ; j += i2) {
                mu[j] = 0;
            }
        }
    }

    long long ans = 0;
    for (int k = 1; k <= SQ; k++) {
        if (mu[k] != 0) {
            ans += (long long)mu[k] * (N / ((long long)k * k));
        }
    }

    cout << ans << endl;
    return 0;
}