added algorithm summary in comments

This commit is contained in:
Krishna Vedala 2020-03-30 08:42:43 -04:00
parent dc7cbdb768
commit 58d1d75958
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
1 changed files with 10 additions and 1 deletions

View File

@ -3,12 +3,21 @@
#include <math.h>
long count_divisors(long long n)
/*
If x = a * b, then both a and b are divisors of x.
Since multiplication is commutative, we only need to search
till a maximum of a=b = a^2 i.e., till sqrt(x).
At every integer till then, there are eaxctly 2 divisors
and at a=b, there is only one divisor.
*/
{
long num_divisors = 0;
for (long long i = 1; i < sqrtl(n) + 1; i++)
if (n % i == 0 && i * i != n)
if (n % i == 0)
num_divisors += 2;
else if (i * i == n)
num_divisors += 1;
return num_divisors;
}