diff --git a/src/algebra/divisors.md b/src/algebra/divisors.md index eabc485d0..2356b0a9c 100644 --- a/src/algebra/divisors.md +++ b/src/algebra/divisors.md @@ -29,8 +29,7 @@ A way of thinking about it is the following: * If there are two distinct prime divisors $n = p_1^{e_1} \cdot p_2^{e_2}$, then you can arrange all divisors in form of a tabular. $$\begin{array}{c|ccccc} -& 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\ -\hline +& 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\\hline 1 & 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\ p_1 & p_1 & p_1 \cdot p_2 & p_1 \cdot p_2^2 & \dots & p_1 \cdot p_2^{e_2} \\\\ p_1^2 & p_1^2 & p_1^2 \cdot p_2 & p_1^2 \cdot p_2^2 & \dots & p_1^2 \cdot p_2^{e_2} \\\\ @@ -42,18 +41,39 @@ So the number of divisors is trivially $(e_1 + 1) \cdot (e_2 + 1)$. * A similar argument can be made if there are more then two distinct prime factors. + +```cpp +long long numberOfDivisors(long long num) { + long long total = 1; + for (int i = 2; (long long)i * i <= num; i++) { + if (num % i == 0) { + int e = 0; + do { + e++; + num /= i; + } while (num % i == 0); + total *= e + 1; + } + } + if (num > 1) { + total *= 2; + } + return total; +} +``` + ## Sum of divisors We can use the same argument of the previous section. * If there is only one distinct prime divisor $n = p_1^{e_1}$, then the sum is: - + $$1 + p_1 + p_1^2 + \dots + p_1^{e_1} = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1}$$ * If there are two distinct prime divisors $n = p_1^{e_1} \cdot p_2^{e_2}$, then we can make the same table as before. The only difference is that now we now want to compute the sum instead of counting the elements. It is easy to see, that the sum of each combination can be expressed as: - + $$\left(1 + p_1 + p_1^2 + \dots + p_1^{e_1}\right) \cdot \left(1 + p_2 + p_2^2 + \dots + p_2^{e_2}\right)$$ $$ = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1}$$ @@ -62,6 +82,33 @@ $$ = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1}$$ $$\sigma(n) = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1} \cdots \frac{p_k^{e_k + 1} - 1}{p_k - 1}$$ +```cpp +long long SumOfDivisors(long long num) { + long long total = 1; + + for (int i = 2; (long long)i * i <= num; i++) { + if (num % i == 0) { + int e = 0; + do { + e++; + num /= i; + } while (num % i == 0); + + long long sum = 0, pow = 1; + do { + sum += pow; + pow *= i; + } while (e-- > 0); + total *= sum; + } + } + if (num > 1) { + total *= (1 + num); + } + return total; +} +``` + ## Multiplicative functions A multiplicative function is a function $f(x)$ which satisfies pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy