The divisor function represented as
example:
The numbers that divide
Important observations
- if
is a prime number then , also because every power of is a divisor of , e.g. - if
is a product of two distinct primes, say then , also - in general let
then where is a prime factor that divides
example:
int number_of_divisors(int n) {
int total = 1;
for (int i = 2; i * i <= n; i += 1) {
int power = 0;
while (n % i == 0) {
power += 1;
n /= i;
}
total *= (power + 1);
}
if (n > 1){
total *= 2;
}
return total;
}
Sum of divisors
The sum of divisors is another important quantity represented by
examples:
So when
another example:
when
Important observations
- if
is a prime number then since the only divisors of a prime number are and - if
is a prime number then because every power of is a divisor of , e.g.
Consider
multiplying the expression by
subtracting
factoring
hence
-
if
is a product of two distinct primes say then , also -
in general let
then where is a prime factor that divides
example:
int sum_of_divisors(int n) {
int total = 1;
for (int i = 2; i * i <= n; i += 1) {
if (n % i == 0) {
int primePower = i;
while (n % i == 0) {
primePower *= i;
n /= i;
}
// sigma(n^k) = (n^{k + 1} - 1) / (k - 1)
total *= (primePower - 1) / (i - 1);
}
}
if (n > 1) {
// if `n` is still a prime number after factorization
// sigma(n) = 1 + n
total *= (1 + n);
}
return total;
}