Note: Factors and prime factorization are two different things. The factors of a number are the numbers that are divisible by it. Example: 12: {1, 2, 3, 4, 6, 12}.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Note: Factors and prime factorization are two different things. The factors of a number are the numbers that are divisible by it. Example: 12: {1, 2, 3, 4, 6, 12}.
Name |
---|
Yes
Sir, thank you bro sir.
To do so, you need to first prime factorize the number. This can be done in $$$O(n)$$$ precomputation time and $$$O(\log n)$$$ per query. You can do so by keeping the smallest prime divisor of each number (for more details see here).
Now that you have the number in its prime factorized form $$${p_1}^{a_1}{p_2}^{a_2}{p_3}^{a_3}\ldots$$$, you can generate factors using a recursive approach. First note that all factors of this number are prime factorized to $$${p_1}^{b_1}{p_2}^{b_2}{p_3}^{b_3}\ldots$$$ where $$$0 \leq b_i \leq a_i$$$. You can recursively generate all sequences of $$$b$$$ that satisfy this requirement and now you have all factors of the number.
Note that this works in $$$O(\log n + k)$$$ time, where $$$k$$$ is the number of factors this number has. Check out highly composite numbers, which is an upper bound to $$$k$$$. I don't know of any ways to solve this problem without the $$$O(n)$$$ precomputation though.
A number can have more than $$$O(\log{n})$$$ divisors ($$$O(\sqrt[3]{n})$$$ is a useful bound in practice, the actual bound is a little more involved, see https://math.stackexchange.com/questions/63687/bound-for-divisor-function ), and an algorithm that finds all divisors is bounded below by the number of divisors, so there is no algorithm for finding divisors in $$$O(\log{n})$$$ time.
A number has $$$O(\log{n})$$$ prime factors, so you might be able to use that.
Thanks for the informative explanation!