Sunday, October 17, 2010

Finding the number of Divisors of a Number

You can say that I've hacked through high school system somehow so I didn't know this before. Today, I was trying to solve a problem that I faced in my last day's DIUPC 2010. While solving the problem I found some interesting properties of number that of course kids play for most. Anyways. The facts that I found are as follows:

1. To find if a number is dividable by other number (of course less that it's half) you just need to check till its' square root. Say if the number is N then check till sqrt(N). That because if the number is dividable by numbers bigger than that then you get the same numbers that you used previously to divide it. Just as this. Say we are testing with 9.
Limit = sqrt(9) = 3.
9/1 = 9
9/3 = 3
9/9 = 1 we used that before. 9 is not a good example so let's use 16.
Limit = sqrt(16) = 4.
16/1 = 1
16/2 = 8
16/4 = 4
16/8 = 2 used.
16/16 = 1 used.
Test with other number to find that I've been a stupid all the way till this day.

2. It's not something that I found today but should be noted here. A number is a combination of several primes. Like this:
1x2 = 2
1x2x3 = 6
1x2x3x4[2x2] = 24
No, 1 is not among primes.

3. If a number N is square then it has equal number of factors below and beyond sqrt(N) + the sqrt(N) itself. Check for 4. sqrt(4) = 2. The divisors of 4 are 1, 2, 4. Check for 16. sqrt(16) = 4. The divisors of 16 are 1, 2, 4, 8, 16. But if the number is not square then It doesn't have the same property. Take 15, the divisors of 15 are 1, 3, 5, 15. Below sqrt(15) are 1, 3 and including and above it are 5, 15. It should've had another number to have the same property.

4. If you keep on dividing a number by it's prime factors (till sqrt(N)) and that number is not a square the at the end there will be a prime left out. Such as 6. Divide by 2. Left 3, But next prime 3 is more squared. So left is 3. Take 20, keep dividing by 2.
20/2 = 10
10/2 = 5
End 'cause 5*5=25>5.
So left is 5.
Take 65.
65/3 = 13
End 'cause it's a prime.

I used this program to find these properties which I did during solving a divisor based problem:

int nod(int n) {

int i, j, c, count=1, temp;

for (j=0, c=0 ; primes[j]*primes[j]<=n ; j++) {
c=1;
temp=primes[j];
while (n%temp==0) {
n/=temp;
c++;
}
count*=c;
}

if (n==1) { /*putchar('x');*/ return count; }
else return count*2;
}


This can be easily modified to show all the primes. Check the sections where I multiply the primes and the sections where I decide what to give as output.

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...