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 Apr 19, 2026
Problem #0193
Level Level 07
Solved By 3,987
Languages C++, Python
Answer 684465067343069
Length 232 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 Foundation

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

μ(n)2=d2nμ(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 d2pd^2 \mid p are only d=1d = 1, so d2pμ(d)=μ(1)=1\sum_{d^2 \mid p} \mu(d) = \mu(1) = 1. case\square_{\text{case}}
  • For k2k \geq 2: μ(pk)2=0|\mu(p^k)|^2 = 0. The divisors dd with d2pkd^2 \mid p^k are d{1,p,,pk/2}d \in \{1, p, \ldots, p^{\lfloor k/2 \rfloor}\}. For k2k \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 j2j \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=1Nμ(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=1Nd2nμ(d)=d=1Nμ(d)nNd2n1=d=1Nμ(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 d2nd^2 \mid n and nNn \leq N imply dNd \leq \sqrt{N}. \square

Lemma 1. (Mobius Sieve.) The values μ(k)\mu(k) for 1kM1 \leq k \leq M can be computed in O(MloglogM)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 pMp \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 pMM/p=O(MloglogM)\sum_{p \leq M} M/p = O(M \log \log M) by Mertens’ theorem. \square

Editorial

Count squarefree numbers below 2^50. Uses: Q(N) = sum_{k=1}^{sqrt(N)} mu(k) * floor(N/k^2). We sieve Mobius function. We then iterate over p from 2 to M. Finally, iterate over k from p to M step p.

Pseudocode

Sieve Mobius function
for p from 2 to M
for k from p to M step p
Compute Q(N)
for k from 1 to M

Complexity Analysis

  • Time: O(NloglogN)O(\sqrt{N} \log \log \sqrt{N}) for the Mobius sieve, plus O(N)O(\sqrt{N}) for the summation. Total: O(NloglogN)O(\sqrt{N} \log \log \sqrt{N}).
  • Space: O(N)O(\sqrt{N}) for storing the μ\mu values. For N=250N = 2^{50}, N=2253.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;
}