1. ## Divisor Algorithm

I have written a workable divisor algorithm that does this:

In order to get all divisors of a some number called "Num":

You search in the the range (1, num/2 + 1) and then do a for loop:

something like this:

for (int i =0; i < num; i ++){
if (num%i) == 0);
//add to the list of divisors

Someone else told me I need to make the range up to num/2 + 1 rather than num/2. Why the +1?

2. > Why the +1?
Because integer division truncates.

3. For example, if num == 20, then 20/2 is 10, and if your loop specifies < 10, then you won't find 10 as a divisor for 20. Therefore, you add 1 to 10, and now you will find 10 as a divisor.

4. You only need to loop up to sqrt(num). The other divisors can be found by then dividing num with the divisors you find within that range.

Code:
```int i, count = 0;

for( i = 0; i < sqrt(num); i++)
{
if(num % i == 0)
{
// Add i to list of divisors.
// Add num / i to list of divisors.
count ++;
}
}

count *= 2;```
This code will give you all divisors as well as the count, with twice as little work.

5. Originally Posted by IceDane
with twice as little work.
as in half the work?

QuantumPete

6. Originally Posted by QuantumPete
as in half the work?

QuantumPete
Pedantic semantics, but should have known - board full of programmers. Haha.

7. Hmmmmmmmmmmm

Fixed:
Code:
```int i, count = 0, square;

for( i = 0, square = sqrt(num); i < square; i++)
{
if(num &#37; i == 0)
{
// Add i to list of divisors.
// Add num / i to list of divisors.
count ++;
}
}
count *= 2;```