Projecteuler -- 21 (Amicable numbers)

Solution :  If you don't understand the solution process I will recommend you to go my blog's algorithm section and search "Divisor কথন" and read it hope you will understand.


problem Description : Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.



/// Solved By Shohanur Rahaman
#include<stdio.h>
#include<math.h>
int sumofdiv(int n);
int main()
{
int i,j;
int n,count=0,div1=1,tmp=0,div2=0;
int sum=0;
n=1;
while(n<=10000){
int root=sqrt(n);
for(i=2;i<=root;i++){
if(n%i==0){
div1=div1+i;
if(i!=n/i)
div1=div1+(n/i);
}
}
tmp=sumofdiv(div1);
if(tmp==n && tmp!=div1){
sum=sum+div1;
printf("Div1 : %d Tmp: %d \n",div1,tmp);
}
div1=1;
n++;
}
printf("\n sum : %d\n",sum);
return 0;
}
int sumofdiv(int n)
{
int i,div=1;
int root=sqrt(n);
for(i=2;i<=root;i++){
if(n%i==0){
div=div+i;
if(i!=n/i){
div=div+n/i;
}
}
}
return div;
}