Constexpr factors_of
factors_of
I’m just now getting around to blogging this snippet from March 2024 (hat tip to Chip Hogg). Apparently some units libraries — things like Au and mp-units — find it useful to represent linear combinations of units as the products of primes; for example, if you represent “meters” as 2 and “seconds” as 5, then “meter-seconds” is 10. Or at least that’s the general idea, I think. Don’t quote me on that part.
This means there’s a market for fast compile-time versions of the functions next_prime_after(p)
and prime_factors_of(c)
. Chip even went so far as to propose that vendors should provide
“largest prime factor of c
” as a builtin; see P3133 “Fast first-factor finding function”
(February 2024). I said, “You don’t need the vendor to do your factoring; you can write that in
general-purpose C++ already!”