I have to return two numbers whose factorial sum is equal to 10!.Two numbers should be returned in array. I have done code as below but it could not found any such two numbers. It ends with "stack overflow exception". My code is:
private int[] solve10()
{
int[] n = new int[2];
bool found=false;
int c1 = 1;
int fact1 = 0;
int fact2 = 0;
int fact10 = 0;
try
{
fact10 = findFactorial(10);
while(!found)
{
fact1 = findFactorial(c1);
for (int j = 1; j < 10;j++)
{
fact2 = findFactorial(j);
if (fact1+fact2==fact10)
{
n[0] = c1;
n[1] = j;
found = true;
break;
}
}
c1++;
}
return n;
}
catch (Exception ex)
{
throw ex;
}
}
findFactorial is a function that returns factorial. Whose definition is as below
private int findFactorial(int n)
{
int fact = 0;
try
{
if(n==0 || n==1)
{
fact= 1;
}
else
{
fact= n * findFactorial(n - 1);
}
return fact;
}
catch (Exception ex)
{
throw ex;
}
}