第一版:定义循环大M和大N,数组存储循环计算结果,遍历数组大M和大N对比寻找亲和数2024.7.20
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int M,N;
int sumgroupM[30000] = { 0 }, sumgroupN[30000] = {0};
for (M = 1;M<30000; M++)//大M循环
{
int sumM = 0;
int groupM[30000] = { 1,0 };
int i = 1, countM = 1;
for (int b = 2; b <= M/2; b++)
{
for (int c = 2; c <= M/2; c++)
{
if ( b * c == M)
{
groupM[i] = b;
i++;
groupM[i] = c;
countM += 1;
break;
}
}
}
if (countM == 1)
continue;
//printf("M的值为:%d\n", M);
for (i = 0; i < countM; i++)
{
sumM += groupM[i];
//printf("%d ",groupM[i]);
}
//printf("\n");
//printf("M的真因数之和为:%d\n\n\n", sumM);
sumgroupM[M] = sumM;
printf("M测算中:%d\n",M);
//Sleep(1);
system("cls");
}
for (N = 1; N < 30000; N++)//大N循环
{
int sumN = 0;
int groupN[30000] = { 1,0 };
int i = 1, countN = 1;
for (int b = 2; b <= N / 2; b++)
{
for (int c = 2; c <= N / 2; c++)
{
if (b * c == N)
{
groupN[i] = b;
i++;
groupN[i] = c;
countN += 1;
break;
}
}
}
if (countN == 1)
continue;
//printf("N的值为:%d\n", N);
for (i = 0; i < countN; i++)
{
sumN += groupN[i];
//printf("%d ", groupN[i]);
}
sumgroupN[N] = sumN;
printf("N测算中:%d\n", N);
system("cls");
}
for (int j = 1; j < 30000; j++)
{
for (int k = 1; k < 30000; k++)
{
if (sumgroupM[j]!=0&&sumgroupN[k]!=0&&j == sumgroupN[k]&&sumgroupM[j]==k&&j!=k)
{
printf("%d和%d互为亲和数\n", j, k);
sumgroupM[j] = 0;
sumgroupM[k] = 0;
}
}
}
return 0;
}
第二版:定义循环大M,数组存储循环计算结果,二次遍历数组寻找亲和数
2024.7.20
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
int M,N;
int sumgroupM[10000] = { 0 }, sumgroupN[10000] = { 0 };
for (M = 1;M<10000; M++)//大M循环
{
int sumM = 0;
int groupM[10000] = { 1,0 };
int i = 1, countM = 1;
for (int b = 2; b <= M/2; b++)
{
for (int c = 2; c <= M/2; c++)
{
if ( b * c == M)
{
groupM[i] = b;
i++;
groupM[i] = c;
countM += 1;
break;
}
}
}
if (countM == 1)
continue;
printf("M的值为:%d\n", M);
for (i = 0; i < countM; i++)
{
sumM += groupM[i];
printf("%d ",groupM[i]);
}
printf("\n");
printf("M的真因数之和为:%d\n\n\n", sumM);
sumgroupM[M] = sumM;
//printf("M测算中:%d\n",M);
//Sleep(1);
//system("cls");
}
for (int j = 1; j < 10000; j++)
{
for (int k = 1; k < 10000; k++)
{
if (sumgroupM[j] != 0 &&sumgroupM[k]!=0 && j == sumgroupM[k] &&k==sumgroupM[j] && j != k)
{
printf("%d和%d互为亲和数\n", j, k);
sumgroupM[j] = 0;
sumgroupM[k] = 0;
}
}
}