Chinaunix首页 | 论坛 | 博客
  • 博客访问: 223161
  • 博文数量: 68
  • 博客积分: 3120
  • 博客等级: 中校
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-08 09:53
文章分类
文章存档

2012年(29)

2011年(3)

2010年(18)

2009年(18)

我的朋友

分类: C/C++

2012-01-25 16:31:19

问题:

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.


答案:31626
#include

int sumDiv(int n)
{
    int i = 2;
    int sum = 1;

    if (n == 1 || n == 2 || n == 3)
        return 1;
    else
    {
        for (i = 2; i <= n/2; i++)
            if (n % i == 0)
                sum += i;
        return sum;
    }
}


int main(void)
{
    int i = 0, sum = 0;
    int sd[10001] = {0};

    for (i=1; i<=10000; i++)
        sd[i] = sumDiv(i);

    for (i=2; i<=10000; i++)
        if (sd[i]!=1 && i!=sd[i]&& sd[i]<=10000 && i == sd[sd[i]] )
            printf("%d ", i),sum += i;

    printf("The sum is %d\n", sum);

    return 0;
}

阅读(747) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~