完全数

问题

完全数: 如果一个数恰好等于其因子之和,则称该数为完全数。求1000以内的全部完全数

分析

穷举。

解决方案

 1:  /**
 2:   * @file   025c.c
 3:   * @author Chaolong Zhang <emacsun@163.com>
 4:   * @date   Sat May 25 11:22:27 2013
 5:   */
 6:  
 7:  #include <stdio.h> 
 8:  
 9:  int main(int argc, char *argv[])
10:  {
11:      int n;
12:      int i;
13:  
14:      for (n=0; n <= 1000; ++n)
15:      {
16:          int sum=0;   
17:          int temp=n;
18:  
19:          for (i=1; i< n ;i++ )
20:          {
21:              if ( 0==n%i  )
22:              {
23:                  sum+=i;
24:                  temp=temp/i;
25:              }
26:          }
27:         if (sum == n )
28:         {
29:             printf ("%d\n",n);
30:         }
31:      }
32:      return 0;
33:  }
34:  

结果

0
1
6
28
496
posted @ 2013-05-25 13:10  emacsun  阅读(290)  评论(0编辑  收藏  举报