很急的——————
要求找出1~~1000内的亲密数对
(亲密数对是指若正整数A的所有因子(包括1但不包括自身,下同)之和为B,而B的因子之和为A,则称A和B为一对亲密数)
虽然算法简单,但还是要细心一点,不然有些数就会错过的
#include <stdio.h>
#include <math.h>
#define N 10000
int factorsum( int n )
{
int i, sum = 1;
int m;
m = (int)sqrt(n);
for( i = 2; i <= m; i ++ )
{
if( n % i == 0 )
{
sum += i + n / i;
}
}
if( m * m == n )
sum -= m;
return sum;
}
void main()
{
int num1, num2, temp, lastresult = 0;
for( num1 = 2; num1 <= N; num1 ++ )
{
if( num1 == lastresult )
continue;
num2 = factorsum(num1);
if( num2 > 1 && num2 <= N )
{
temp = factorsum(num2);
if( temp == num1 )
{
lastresult = num2;
printf( "%d\t%d\n", num1, num2 );
}
}
}
}