Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244338
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-08 23:30:43

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.

--------------------
  1. #include <stdio.h>

  2. unsigned d(int num)
  3. {
  4.     int i, sum = 0;

  5.     for (i=1; i < num/2+1; i++) {
  6.         if (num%i == 0)
  7.             sum += i;
  8.     }

  9.     return sum;
  10. }

  11. #define N 10000
  12. int main(int argc, const char *argv[])
  13. {
  14.     int i, sum = 0;
  15.     unsigned d1, d2;

  16.     for (i=2; i<N; i++) {
  17.         d1 = d(i);
  18.         d2 = d(d1);
  19.         if (d2 == i && i != d1 )
  20.             sum += i;
  21.     }

  22.     printf("sum: %d\n", sum);

  23.     return 0;
  24. }

  25. 效率不高,还有很大的优化空间。


阅读(1382) | 评论(0) | 转发(0) |
0

上一篇:euler19

下一篇:euler22

给主人留下些什么吧!~~