Finding Divisors,
Number of Divisors
and
Summation of Divisors
by
Jane Alam Jan
Finding Divisors
If we observe the property of the numbers, we will find some interesting facts. Suppose we have a number 36.
36 is divisible by 1, 2, 3, 4, 6, 9, 12, 18 and 36. It is clear that if a is divisible by b, then a is also divisible by (a/b). That means, 36 is divisible by 3, so, this property helps us to find that 36 will also be divisible by 12 (36/3). Now we will make two lists, if a is divisible by b, then we will take b into the first list and (a/b) into the second list.
So, for 36, we will evaluate our idea.
36 is divisible by 1. So, it will be added in list 1, and (36/1) = 36 will be added in list 2.
List 1: 1
List 2: 36
36 is divisible by 2. So, it will be added in list 1, and (36/2) = 18 will be added in list 2.
List 1: 1 2
List 2: 36 18
36 is divisible by 3. So, it will be added in list 1, and (36/3) = 12 will be added in list 2.
List 1: 1 2 3
List 2: 36 18 12
36 is divisible by 4. So, it will be added in list 1, and (36/4) = 9 will be added in list 2.
List 1: 1 2 3 4
List 2: 36 18 12 9
36 is divisible by 6. So, it will be added in list 1, and (36/6) = 6 will be added in list 2.
List 1: 1 2 3 4 6
List 2: 36 18 12 9 6
Now, see that if we move further we will find the numbers again (some numbers from list 2, which will try to insert into list 1) So, it is clear that, if we have list 1, we can generate list 2 (from this position).
So, we can stop checking if we find b, for which a/b is less than or equal to b. If we try to divide by numbers greater than b, we would actually repeat the same steps. For 36, (check the lists) the next number that will be included to list 1 is 9, but 36/9 = 4, which is already in list 1.
Now we have to find b for which a/b is less than or equal to b. If we think mathematically,
b >= a/b
or, b^{2} >= a
or, b >= sqrt(a)
so, b_{minimum} = sqrt(a)
So, we have already found an idea to find divisors of a number in complexity - sqrt(N). And of course, we can count them to find the number of divisors and summing them to find the summation of divisors.
Finding Number of Divisors
Now, we want to find the number of divisors of an integer. It can be done easily by finding all the divisors and finally counting them as described above. But there is a simpler idea. If we know the prime factorization of a number, we can easily find the number of divisors.
For example, 18 has 6 divisors. If we prime factorize 18, we find that
18 = 2^{1} * 3^{2}
We can say that any divisor of 36 should be in the form
2^{x} * 3^{y} where 0 <= x <= 1 and 0 <= y <= 2
This can be proved easily, but if we think a bit we will find that the reason is obvious.
So, the divisors are,
2^{0} * 3^{0} = 1
2^{0} * 3^{1} = 3
2^{0} * 3^{2} = 9
2^{1} * 3^{0} = 2
2^{1} * 3^{1} = 6
2^{1} * 3^{2} = 18
That means we are taking all possible solutions. These form the number of divisors. Just see the pattern of the prime factors of the divisors. So, we can say that the number of divisors of 18 is
(1 + 1) * (2 + 1) = 6
So, if a number is factorized as
2^{x} * 3^{y}
Then the number of divisors is (x+1) * (y+1). Again, the reason is
So, there are (x+1) sections each having (y+1) divisors, which means a total of (x+1) * (y+1) divisors.
Now, let's find the total number of divisors of 60.
60 = 2^{2} * 3 * 5
So, similarly the divisors are
2^{0} * 3^{0} * 5^{0} = 1
2^{0} * 3^{0} * 5^{1} = 5
2^{0} * 3^{1} * 5^{0} = 3
2^{0} * 3^{1} * 5^{1} = 15
2^{1} * 3^{0} * 5^{0} = 2
2^{1} * 3^{0} * 5^{1} = 10
2^{1} * 3^{1} * 5^{0} = 6
2^{1} * 3^{1} * 5^{1} = 30
2^{2} * 3^{0} * 5^{0} = 4
2^{2} * 3^{0} * 5^{1} = 20
2^{2} * 3^{1} * 5^{0} = 12
2^{2} * 3^{1} * 5^{1} = 60
So, total number of divisors is 12. Let's check it in another way.
For each section of 2 (2^{0}, 2^{1} or 2^{2}) the number of divisors are same. So, there are 3 sections for 2. Now for each section of 2, there are two sections for 3 (3^{0}, 3^{1}). Now for any combination of 2 and 3 there are two sections for 5 (5^{0}, 5^{1}). So, it can be represented as
(2^{0}, 2^{1}, 2^{2}) and (3^{0}, 3^{1}) and (5^{0}, 5^{1})
So, at first take any power of 2, after that take any power of 3 and after that take any power of 5 from the above sections. So, we can take 2 in three ways, 3 in two ways and 5 in two ways. So, total ways (or total divisors) are
3 * 2 * 2
= 12
Similarly, suppose a number is factorized as 2^{x} * 3^{y }* 5^{z}, so, the total number of divisors is
(x + 1) * (y + 1) * (z + 1)
Now, the general formula is
If the prime factorization of an integer is
p_{1}^{x1} * p_{2}^{x2} * p_{3}^{x3} * ... * p_{n}^{xn}
where, p_{1}, p_{2}, ..., p_{n }are primes, then the number of divisors is
(x_{1} + 1) * (x_{2} + 1) * (x_{3} + 1) * ... * (x_{n} + 1)
Finding Summation of Divisors
Suppose we want to find the summation of all the divisors of an integer. We can do it by finding all the divisors and then summing them. But we can make it better. The idea is as follows.
Let's say we want to find the summation of divisors of 60. From previous section, we know that the divisors are 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30 and 60; so the summation is 168. The summation can be written as (summing up all the divisors in their factorized form),
2^{0} * 3^{0} * 5^{0} + 2^{0} * 3^{0} * 5^{1} + 2^{0} * 3^{1} * 5^{0} + 2^{0} * 3^{1} * 5^{1} +
2^{1} * 3^{0} * 5^{0} + 2^{1} * 3^{0} * 5^{1} + 2^{1} * 3^{1} * 5^{0} + 2^{1} * 3^{1} * 5^{1} +
2^{2} * 3^{0} * 5^{0} + 2^{2} * 3^{0} * 5^{1} + 2^{2} * 3^{1} * 5^{0} + 2^{2} * 3^{1} * 5^{1}
It can be written as,
2^{0} * (3^{0} * 5^{0} + 3^{0} * 5^{1} + 3^{1} * 5^{0} + 3^{1} * 5^{1}) +
2^{1} * (3^{0} * 5^{0} + 3^{0} * 5^{1} + 3^{1} * 5^{0} + 3^{1} * 5^{1}) +
2^{2} * (3^{0} * 5^{0} + 3^{0} * 5^{1} + 3^{1} * 5^{0} + 3^{1} * 5^{1})
can be written as
(2^{0} + 2^{1} + 2^{2}) * (3^{0} * 5^{0} + 3^{0} * 5^{1} + 3^{1} * 5^{0} + 3^{1} * 5^{1})
can be written as
(2^{0} + 2^{1} + 2^{2}) * (3^{0} * (5^{0} + 5^{1}) + 3^{1} * (5^{0} + 5^{1}))
can be written as
(2^{0} + 2^{1} + 2^{2}) * (3^{0} + 3^{1}) * (5^{0} + 5^{1})
Now, we know that the summation of series
So, the summation can be written as
So, the result is same as we have calculated.
So, if the prime factorization of an integer is
where, p_{1}, p_{2}, ..., p_{n }are primes, then the summation of divisors is
Discussion
After finding the prime factorization, these ideas are easy to code. Before get to coding, try to understand them fully.
Developed and Maintained by
JANE ALAM JAN |
Copyright © 2012
LightOJ, Jane Alam Jan |