# How to write simple mathematical programs in C# ....1

**This article aims to provide a solution to a simple mathematical problem.. how to arrive at "amicable numbers" using C# as the tool. It uses the basic algebra of C# loops, variables and arraylists, all very basic to solve an interesting anomaly of the mathematical world.**

I want to share with you how C# can be used to solve fun problems in the mathematical world.

Here I use some simple C# constructs like loops, ref variables and arraylists to achieve a practical solution.

The solution is divided into 2 methods both of which constitute steps in the solution.

Comments have been added listing the significance of each step in easy to understand language.

This article is aimed at an audience that has grasped the essential elements of C# and is looking for some avenues to "get dirty with code".

I wish to emphasize that this is my original solution and is not plagiarized from some forum or newsgroup.

The solution is not "rocket science". It is realistic and works well.

Try out the code and work backwords from the solution.

By reverse engineering the code you will arrive at my thought process.

Not very hard to fathom.

As an example consider "amicable numbers".

Amicable numbers are defined as a pair of numbers, each of which is the sum of the factors of the other (e.g. 220 and 284).

How can we find such numbers in C#.

Here is my original solution.

First the routine itself.

This function lists the factors of a number, excluding the number itself and returns the sum through the ref parameter.

private void GetFactorDetails(double num, ref double numsum)

{

double n = num; //here we identify the input

double j = 2; // start number

double k = 0; //initialize variable

double fcount = 1; //initializer

ArrayList ar = new ArrayList(); //an arraylist remember to refer to the namespace System.Collections

while (j < n) // loop from 2 to n but don't include n itself (please note)

{

k = (n % j); // is the number perfectly divisible i.e. is j a factor

if (n != j) // all factors not including the input

{

if ((int)k == 0)

{

fcount += j; j++; numsum = fcount; continue; //take the sum of the factors (as required in the problem statement)

}

else

{

j++; continue; // skip else

}

}

n++;

fcount = 0;

numsum = 0;

if ((int)n == 1000) break; // an arbitrary limit has been set for convenience

}//loop

}

Then the calling routine.

Here we call the above routine twice (to implement the above logic).

If the number in the first call equals the sum of the factors in the second call, we have our numbers.

//Amicable Numbers

ArrayList ar = new ArrayList();

double snum = 0;

double tnum = 0;

for (int jj = 1; jj < 2000; jj++)

{

GetFactorDetails(jj, ref snum); //.....(1) jj is the input and snum is the sum of the factors

GetFactorDetails(snum, ref tnum); //....(2) snum is the input and tnum is the sum of the factors

if ((jj == tnum) && (jj != snum)) // here comes the test at last

{

if (!(ar.Contains(jj.ToString()))) // dont add the same number twice

{

ar.Add(jj.ToString());

}

if (!(ar.Contains(snum.ToString()))) // similar to above

{

ar.Add(snum.ToString());

}

}

}

for (int jk = 0; jk < ar.Count; jk++)

{

MessageBox.Show(ar[jk].ToString()); // show output

}

It's as simple as that. Funny, if you think some complex algorithm is involved.

Easy as....1 2.3.