Skip to content

Commit 15ff2e9

Browse files
authored
Merge pull request #1127 from md-shamim-ahmad/patch-1
Update divisors.md
2 parents e15ddf4 + f774c39 commit 15ff2e9

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

src/algebra/divisors.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ A way of thinking about it is the following:
2929
* 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.
3030

3131
$$\begin{array}{c|ccccc}
32-
& 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\
33-
\hline
32+
& 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\\hline
3433
1 & 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\
3534
p_1 & p_1 & p_1 \cdot p_2 & p_1 \cdot p_2^2 & \dots & p_1 \cdot p_2^{e_2} \\\\
3635
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)$.
4241

4342
* A similar argument can be made if there are more then two distinct prime factors.
4443

44+
45+
```cpp
46+
long long numberOfDivisors(long long num) {
47+
long long total = 1;
48+
for (int i = 2; (long long)i * i <= num; i++) {
49+
if (num % i == 0) {
50+
int e = 0;
51+
do {
52+
e++;
53+
num /= i;
54+
} while (num % i == 0);
55+
total *= e + 1;
56+
}
57+
}
58+
if (num > 1) {
59+
total *= 2;
60+
}
61+
return total;
62+
}
63+
```
64+
4565
## Sum of divisors
4666
4767
We can use the same argument of the previous section.
4868
4969
* If there is only one distinct prime divisor $n = p_1^{e_1}$, then the sum is:
50-
70+
5171
$$1 + p_1 + p_1^2 + \dots + p_1^{e_1} = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1}$$
5272
5373
* 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.
5474
The only difference is that now we now want to compute the sum instead of counting the elements.
5575
It is easy to see, that the sum of each combination can be expressed as:
56-
76+
5777
$$\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)$$
5878
5979
$$ = \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}$$
6282
6383
$$\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}$$
6484
85+
```cpp
86+
long long SumOfDivisors(long long num) {
87+
long long total = 1;
88+
89+
for (int i = 2; (long long)i * i <= num; i++) {
90+
if (num % i == 0) {
91+
int e = 0;
92+
do {
93+
e++;
94+
num /= i;
95+
} while (num % i == 0);
96+
97+
long long sum = 0, pow = 1;
98+
do {
99+
sum += pow;
100+
pow *= i;
101+
} while (e-- > 0);
102+
total *= sum;
103+
}
104+
}
105+
if (num > 1) {
106+
total *= (1 + num);
107+
}
108+
return total;
109+
}
110+
```
111+
65112
## Multiplicative functions
66113

67114
A multiplicative function is a function $f(x)$ which satisfies

0 commit comments

Comments
 (0)
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