c++打卡练习(23)

亲密数

如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子
(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。求3000以内的全部亲密数。

流程图:

伪代码:

源代码:

#include<iostream>
using namespace std;
int main (){
int a,i,b,n;
printf("There are following friendly--numbers pair smaller than 3000:\n");
for(a=1;a<3000;a++){
b=0,n=0;
for(i=1;i<=a/2;i++){
if(!(a%i))
b+=i;
}
for(i=1;i<=b/2;i++){
if(!(b%i))
n+=i;
}
if(n==a&&a<b)
printf("%4d--%4d ",a,b);
}
return 0;
}

 

posted @ 2023-05-09 21:50  夏季彼岸德  阅读(39)  评论(0)    收藏  举报