Lemma
Proof
Let .
Du’s First Multiplication Sieve is an algorithm that computes for arithmetic functions and , if and are given, in time and space.
Algorithm
Lemma
Proof
Applying the lemma to find yields an algorithm that solves the problem in time and space.
std::unordered_map<int, int> du(int n, const std::unordered_map<int, int> &sf, const std::unordered_map<int, int> &sg) {
int m = std::sqrt(n);
std::vector<int> d;
for (int i = 1; i < m; i++) {
if (n / (n / i) == i) {
d.push_back(i);
}
}
for (int i = n / m; i > 0; i--) {
d.push_back(n / i);
}
d.erase(std::unique(d.begin(), d.end()), d.end());
std::unordered_map<int, int> sh;
for (int i : d) {
for (int j = 1; j <= i; j = i / (i / j) + 1) {
sh[i] += (sg.at(i / (i / j)) - (j > 1 ? sg.at(j - 1) : 0)) * sf.at(i / j);
}
}
return sh;
}Proof
Applying the lemma yields that this algorithm solves the problem in
time.
Since
and
it follows that
Therefore,