实例四十二:因子和问题
问题描述:
输出 1-N (N<32 768) 之间所有同时满足下列条件的整数对(A,B):
(1)3<A<B<=N;
(2)A 的因子之和等于 B,且 B 的因子之和等于 A ( X 的因子和是指除了 X 以外的所有因子之和)。
Outputs an integer pair (A, B) between 1-N (N<32 768) that satisfies the following conditions:
(1)3<A<B<=N;
(2) The sum of the factors of A is equal to B, and the sum of the factors of B is equal to A (the factor sum of X refers to the sum of all factors except X).
算法思路:
先求出 4~N 之间的任意数 A 的因子和 X,若 A<X<=N,且 X 的因子和等于 A ,则(A,X)即为满足条件的解;否则,就不存在与 A 配对的 B。
First find the factor of any number A between 4 and N and X. If A<X<=N, and the factor sum of X is equal to A, then (A, X) is the solution that satisfies the condition; otherwise, there is no pairing with A. B.
/*实例四十二:因子和问题*/
#include <stdio.h>
int sumy(int n)
{
int i,j,s;
s=1;
i=2;
do
{
j=n/i;
if(n%i==0)
if(i==j)
s+=i;
else //可能产生歧义2019.3.31
s+=i+j;
i++;
}while(i<j);
return s;
}
int main(void)
{
int i,x,n;
printf("Please enter the integer value of n:\n");
scanf("%d",&n);
for(i=4;i<n;i++)
{
x=sumy(i);
if(x>i&&x<=n)
if(i==sumy(x))
printf("%d%d\n",i,x);
}
return 0;
}