现在的位置: 首页 > 综合 > 正文

Problem 21

2012年12月06日 ⁄ 综合 ⁄ 共 771字 ⁄ 字号 评论关闭
文章目录

Amicable numbers

Problem 21

05 July 2002

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.

 

Answer:
31626

 

 1 void p21()
 2 {
 3     long sum=0;
 4     for(int n=1; n<=10000; ++n)
 5     {
 6         int sum_1=1;
 7         for(int i=2;i<n;++i)
 8         {
 9             if(n%i==0)
10                 sum_1+=i;;
11         }
12         if(sum_1>=n || sum_1==1)
13             continue;
14         int sum_2=1;
15         for(int i=2;i<sum_1;++i)
16         {
17             if(sum_1%i==0)
18                 sum_2+=i;
19         }
20         if(sum_2==n)
21         {
22             cout<<"n="<<n<<",sum_1="<<sum_1<<"sum_2="<<sum_2<<endl;
23             sum=sum+n+sum_1;
24         }
25     }
26 
27     cout<<sum<<endl;
28 }

 

 

 

抱歉!评论已关闭.