From 133f1e86367559732213d9f92b09f951735eca13 Mon Sep 17 00:00:00 2001 From: Md Shamim Ahmmed <56692841+Md-Shamim-Ahmmed@users.noreply.github.com> Date: Sat, 22 Jul 2023 07:45:17 +0600 Subject: [PATCH 1/2] Update divisors.md --- src/algebra/divisors.md | 48 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/algebra/divisors.md b/src/algebra/divisors.md index eabc485d0..53ae7d4a0 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 +vector primes; + +long long NumberOfDivisors(long long num) { + long long total = 1; + + for (long long p: primes) { + if (num % p == 0) { + int e = 0; + while (num % p == 0) { + e++; + num /= p; + } + total *= (e + 1); + } + } + 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,26 @@ $$ = \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 +vector primes; + +long long SumOfDivisors(long long num) { + long long total = 1; + + for (long long p: primes) { + if (num % p == 0) { + int e = 0; + while (num % p == 0) { + e++; + num /= p; + } + total *= (pow(p, (e + 1)) - 1) / (p - 1); + } + } + return total; +} +``` + ## Multiplicative functions A multiplicative function is a function $f(x)$ which satisfies From f774c39e8841d73fec7b6fcd80a44447c08985e2 Mon Sep 17 00:00:00 2001 From: Md Shamim Ahmad <56692841+md-shamim-ahmad@users.noreply.github.com> Date: Thu, 24 Aug 2023 20:17:22 +0600 Subject: [PATCH 2/2] Update divisors.md --- src/algebra/divisors.md | 43 ++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/algebra/divisors.md b/src/algebra/divisors.md index 53ae7d4a0..2356b0a9c 100644 --- a/src/algebra/divisors.md +++ b/src/algebra/divisors.md @@ -43,21 +43,21 @@ So the number of divisors is trivially $(e_1 + 1) \cdot (e_2 + 1)$. ```cpp -vector primes; - -long long NumberOfDivisors(long long num) { +long long numberOfDivisors(long long num) { long long total = 1; - - for (long long p: primes) { - if (num % p == 0) { + for (int i = 2; (long long)i * i <= num; i++) { + if (num % i == 0) { int e = 0; - while (num % p == 0) { + do { e++; - num /= p; - } - total *= (e + 1); + num /= i; + } while (num % i == 0); + total *= e + 1; } } + if (num > 1) { + total *= 2; + } return total; } ``` @@ -83,21 +83,28 @@ $$ = \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 -vector primes; - long long SumOfDivisors(long long num) { long long total = 1; - for (long long p: primes) { - if (num % p == 0) { + for (int i = 2; (long long)i * i <= num; i++) { + if (num % i == 0) { int e = 0; - while (num % p == 0) { + do { e++; - num /= p; - } - total *= (pow(p, (e + 1)) - 1) / (p - 1); + 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; } ``` 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