Commit b4b14e21 authored by Adam P. Goucher's avatar Adam P. Goucher

Compute all divisors of any uint64_t.

parent 43c6a072
......@@ -291,6 +291,22 @@ namespace apg {
return mmap;
}
std::vector<uint64_t> divisors64(uint64_t N) {
// Returns a list of all divisors of N, including 1 and N.
// If two divisors multiply to give N, then their positions
// in the list sum to length - 1. [N.B. the divisors do not
// appear in numerical order, instead mimicking a strided
// multidimensional array giving the divisibility lattice.]
std::map<uint64_t, uint64_t> pfac = apg::factorise64(N);
std::vector<uint64_t> divs; divs.push_back(1);
for (auto it = pfac.begin(); it != pfac.end(); ++it) {
uint64_t p = it->first;
uint64_t k = it->second * divs.size();
for (uint64_t i = 0; i < k; i++) { divs.push_back(p * divs[i]); }
}
return divs;
}
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment