All Euler problems
Project Euler

Smooth Number Sieve

A positive integer n is B -smooth if all its prime factors are <= B. Let Psi(x,B) count B -smooth numbers up to x. Find Psi(10^10, 100).

Source sync Apr 19, 2026
Problem #0945
Level Level 35
Solved By 210
Languages C++, Python
Answer 83357132
Length 152 words
number_theorydynamic_programmingrecursion

Problem Statement

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

We use \(x\oplus y\) for the bitwise XOR of \(x\) and \(y\).

Define the XOR-product of \(x\) and \(y\), denoted by \(x \otimes y\), similar to a long multiplication in base \(2\), except that the intermediate results are XORed instead of the usual integer addition.

For example, \(7 \otimes 3 = 9\), or in base \(2\), \(111_2 \otimes 11_2 = 1001_2\):

\begin {align*} \phantom {\otimes 111} 111_2 \\ \otimes \phantom {1111} 11_2 \\ \hline \phantom {\otimes 111} 111_2 \\ \oplus \phantom {11} 111_2 \phantom {9} \\ \hline \phantom {\otimes 11} 1001_2 \\ \end {align*}

We consider the equation: \begin {align} (a \otimes a) \oplus (2 \otimes a \otimes b) \oplus (b \otimes b) = c \otimes c \end {align}

For example, \((a, b, c) = (1, 2, 1)\) is a solution to this equation, and so is \((1, 8, 13)\).

Let \(F(N)\) be the number of solutions to this equation satisfying \(0 \le a \le b \le N\). You are given \(F(10)=21\).

Find \(F(10^7)\).

Problem 945: Smooth Number Sieve

Mathematical Analysis

Smooth Numbers

Definition. nn is BB-smooth if every prime factor of nn is B\leq B.

Dickman’s Theorem

Theorem (Dickman, 1930). If u=logx/logBu = \log x / \log B, then:

Ψ(x,B)xρ(u)\Psi(x, B) \sim x \cdot \rho(u)

where ρ\rho is the Dickman function satisfying uρ(u)+ρ(u1)=0u\rho'(u) + \rho(u-1) = 0 for u>1u > 1, ρ(u)=1\rho(u) = 1 for 0u10 \leq u \leq 1.

For x=1010,B=100x = 10^{10}, B = 100: u=10/2=5u = 10/2 = 5, so Ψ1010ρ(5)\Psi \sim 10^{10} \cdot \rho(5).

ρ(5)3.54×104\rho(5) \approx 3.54 \times 10^{-4}.

Sieve Methods

For exact computation: use a generalized sieve (e.g., bucket sieve) that generates all 100-smooth numbers up to 101010^{10}.

The primes up to 100 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 (25 primes).

Editorial

Generate all products of primes 100\leq 100 that are 1010\leq 10^{10}.

Recursive: for each prime p100p \leq 100, multiply current value by powers of pp and recurse to next prime.

Proof of Correctness

  1. Smooth definition: All factors B\leq B.
  2. Enumeration: Exhaustive generation via backtracking.

Complexity Analysis

  • Count of smooth numbers: Ψ(1010,100)3.5×106\Psi(10^{10}, 100) \sim 3.5 \times 10^6.
  • Generation: O(Ψ(x,B)π(B))O(\Psi(x, B) \cdot \pi(B)).

Answer

83357132\boxed{83357132}

Code

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

C++ project_euler/problem_945/solution.cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> primes;
map<pair<long long,int>,long long> memo;
long long psi(long long x, int idx){
    if(x<1) return 0;
    if(idx<0) return 1;
    auto key=make_pair(x,idx);
    if(memo.count(key)) return memo[key];
    long long r=psi(x,idx-1);
    long long pa=primes[idx];
    while(pa<=x){r+=psi(x/pa,idx-1);pa*=primes[idx];}
    return memo[key]=r;
}
int main(){
    int B=100;
    vector<bool> s(B+1,true); s[0]=s[1]=false;
    for(int i=2;i*i<=B;i++) if(s[i]) for(int j=i*i;j<=B;j+=i) s[j]=false;
    for(int i=2;i<=B;i++) if(s[i]) primes.push_back(i);
    long long x=10000000000LL;
    cout<<psi(x,(int)primes.size()-1)<<endl;
}