Thursday, 7 August 2014

Amicable Numbers sum

//answer=31626
int factor(int n)
{
int x=1;
int sum=0;
while(x<=(n/2))
{
if(n%x==0)
{
    sum+=x;
}
x++;

}
return sum;
}
main()
{int p=1;
int sum=0;
while(p<=10000)
{

    int res=factor(p);
    int ser=factor(res);
    if((ser==p)&&(res!=p)){sum+=p;}
    p++;
}
printf("%d",sum);
}

No comments:

Post a Comment