我重新换了一种算法
这种方法 大概能在16秒左右把40000000之内的 完数扫描出来
#include<stdio.h>
int iswanshu(int n)
{
int i,j,count ;
i=count=0 ;
j=n ;
while(i!=1)
{
i=n/2 ;
if(n%2!=0)
i++;
if(j%i!=0)
{
return 0 ;
}
if((i%2)!=0&&i>1)
count++;
n=i ;
}
if(count>0)
{
return 1;
}else{
return 0;
}
}
int checkagain(int n)
{
int i,sum ;
sum=0 ;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i ;
}
if(sum==n){
return 1 ;
}else{
return 0 ;
}
}
printwanshu(int n)
{
int i ;
i=0 ;
printf("%d its factors are ",n);
while(i!=1)
{
i=n/2 ;
if(n%2!=0)
i++;
printf("%d,",i);
n=i ;
}
printf("\n");
}
main()
{
int i,n ;
while(1)
{
printf("本程序将实现输出1-n之间的完数,n>=1\n现在请输入n:");
scanf("%d",&n);
printf("你输入的数是%d\n",n);
if(n>=1)
break ;
}
for(i=1;i<=n;i++)
{
if(iswanshu(i)==1&&checkagain(i)==1)
printwanshu(i);
}
}
-----------------------------
我输入n=40000000时 大概16左右出了结果如下
6 its factors are 3,2,1,
28 its factors are 14,7,4,2,1,
496 its factors are 248,124,62,31,16,8,4,2,1,
8128 its factors are 4064,2032,1016,508,254,127,64,32,16,8,4,2,1,
33550336 its factors are 16775168,8387584,4193792,2096896,1048448,524224,262112,
131056,65528,32764,16382,8191,4096,2048,1024,512,256,128,64,32,16,8,4,2,1,
[[it] 本帖最后由 gzbao9999 于 2008-11-5 15:18 编辑 [/it]]