怎样找出1000以内的所有完数?帮帮忙,谢谢啦!
一个数如果恰好等于它的因子之和,这个数就叫做完数,例如6的因子是1,2,3,而6=1+2+3,因此6是完数,编程序找出1000之内的所有完数;并按以下格式输出:6 its factors are:1,2,3
/* * 一个数如果恰好等于它的因子之和,那么它就称为完数. * 例如,6的因子为1,2,3,而6=1+2+3,因此 * 6是完数.编程找出1000内的所有完数,并且按照 * 下面格式输出其因子: * 6 its factors are 1,2,3 */ #include <stdio.h> #define SCOPE 1000 /* 1000 have 16 factors: 1,2,4,5,8,10,20,25,40,50,100,125,200,250,500,1000*/ #define N 16 #define TRUE 1 #define FALSE 0 int Factors[N]; int isFactor(int num,int vector) { int result = FALSE; if( num % vector == 0) { result =TRUE; } return result; } main() { int sum,target; int result,i,count; for(target = 2;target<=SCOPE;target++) { sum = 0; count=0; for(i=1;i<=target/2;i++) { if(isFactor(target,i)) { sum += i; Factors[count++] = i;/*把找到的因子结果记录到表中*/ } } if(sum == target) { printf("%d\t",target); printf("its factors are:"); for(i=0;i<count;i++) { printf("%d,",Factors[i]); } printf("\n"); } } }