Problem 21

Statement
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.

Solution
The code, in C.

#include <stdio.h>

int factSum(int a)
{
  int i,b=a,s=1;
  for(i=2;i<b;i++){
    if(!(a%i)){
      s+=i;
      s+=a/i;
      b=a/i;
    }
  }
  return s;
}

int amiSum(int max)
{
  int i,f,s=0;
  for(i=0;i<max;i++){
    f=factSum(i);
    if(i==factSum(f) &amp;&amp; f<max &amp;&amp; i!=f)
      s+=i;
  }
  return s;
}

int main(void)
{
  printf("%d\n",amiSum(10000));
  return 0;
}

Answer
31626

No tags for this post.

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>