Find the amicable numbers - i.e. sum of factors of the first number (except itself) equals the second AND vice versa....(210,284),(1184,1210) etc.
Solution below in C#
//Subroutine
private void GetFactorDetails(double num, ref double numsum)
{
//Positively Described Numbers - Part 1
double n = num;
double j = 2;
double k = 0;
double fcount = 1;
ArrayList ar = new ArrayList();
while (j < n)
{
k = (n % j);
if (n != j)
{
if ((int)k == 0)
{
fcount += j; j++; numsum = fcount; continue;
}
else
{
j++; continue;
}
}
n++;
fcount = 0;
numsum = 0;
if ((int)n == 1000) break;
}//loop
}
//Amicable Numbers
//Calling Routine
ArrayList ar = new ArrayList();
double snum = 0;
double tnum = 0;
for (int jj = 1; jj < 2000; jj++)
{
GetFactorDetails(jj, ref snum);
GetFactorDetails(snum, ref tnum);
if ((jj == tnum) && (jj != snum))
{
if (!(ar.Contains(jj.ToString())))
{
ar.Add(jj.ToString());
}
if (!(ar.Contains(snum.ToString())))
{
ar.Add(snum.ToString());
}
}
}
for (int jk = 0; jk < ar.Count; jk++)
{
MessageBox.Show(ar[jk].ToString());
}
Comments