快速计算因子的和:从2到sqrt,只要%为0就+i+x/i
注意初始为1、sqrt是整数要额外减去
#include<iostream> #include<vector> #include<math.h> using namespace std; int getSumOfFactor(int x){ int sum = 1; int root = sqrt(x); for(int i=2;i<=root;i++){ if(x%i==0){ sum+=i; sum+=x/i; } } if(root*root==x) sum-=root; return sum; } int main(){ vector<int> e; vector<int> g; for(int i=2;i<=60;i++){ int k = getSumOfFactor(i); if(k==i){ e.push_back(i); } else if(k>i){ g.push_back(i); } } vector<int>::iterator it1 = e.begin(); cout<<"E:"; while(it1!=e.end()){ cout<<" "<<*it1; it1++; } vector<int>::iterator it2 = g.begin(); cout<<" G:"; while(it2!=g.end()){ cout<<" "<<*it2; it2++; } return 0; }