java程式
package net.coollf;
public class Main {
public static void main(String[] agrs){
for(INT i=0;i<2000;i++){
if(i==check(check(i))&&i!=check(i)){
System.out.println(i+":"+check(i));
}
}
}
private static int check(int i) {
int sum=1;
for(int k=2;k<Math.sqrt(i);k++){
if(i%k==0){
sum+=k;
if(k*k!=i){
sum+=(i/k);
}
}
}
return sum;
}
}
C語言程式
#include<stdio.h>
#include<math.h>
#define N 400
struct intimate{
int n;
int sum;
int flag;
};
int FactorSum(int fac) // 親密對數計算
{
int sum = 1;
int temp = 0;
int ret = sqrtf(fac);
if(fac == 1)
{
return sum = 1;
}
for(int i = 2; i <= ret; i++)
{
if(0 == fac%i)
{
temp = fac/i;
sum = sum + temp + i;
}
}
if(ret == sqrtf(fac))
{
sum = sum - ret;
}
return sum;
}
int IntimatePair(int range) //查找親密對數輸出
{
int count = 0;
struct intimate inti[N];
for(int i = 0; i < N; i++)
{
inti[i].n = i + 1;
inti[i].sum = FactorSum(i + 1);
inti[i].flag = 0;
}
for(int i = 0; i < N; i++)
printf("inti[%d] = %d\n", i + 1,inti[i].sum);
for(int j = 0; j < N; j++)
{
if(inti[j].flag == 0)
{
for(int k = j + 1; k < N; k++)
{
if(inti[j].sum == inti[k].n && inti[j].n == inti[k].sum && inti[k].flag == 0)
{
inti[j].flag = 1;
inti[k].flag = 2;
printf("%d ,%d\n",inti[j].n,inti[k].n);
}
}
}
}
return count;
}
int main()
{
IntimatePair(N);
return 0;
}