0

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;
        }
    }
Sunil
  • 103
  • 13
  • 1
    This is discussed earlier in [SO Post](http://stackoverflow.com/questions/25135435/c-sum-of-two-factorials-euals-factorial-of-10-find-two-values-say-x-and-y-whos) , what is more interesting is `point 3` in the answer. – Hari Prasad Mar 22 '16 at 04:09
  • Why have you got a `try`/`catch` block in there? What exception are you expecting it will throw that you can recover from? – Enigmativity Mar 22 '16 at 04:19
  • 1
    I didn't notice, but you have a `try`/`catch` in both. Is that how you write all of your code? It's a bad practice. – Enigmativity Mar 22 '16 at 04:20
  • 1
    Now, also mathematically speaking `n! > 2 x (n - 1)!` for all `n > 2`. In other words, there **does not exist** a pair of numbers from `[1..9]` such that the sum of the factorials equals 10 factorial. There are no solutions. – Enigmativity Mar 22 '16 at 04:27
  • Why try catch is not good practice. Yes may be there is no need of try catch in my above code, but I think it is good to use try/catch in case some exceptions. And for my question I think you are right that there is no solution.If there is any solution then I am sure that my code should give answer. I just want to be confirmed if there is any fault and there are such numbers whose factorial sum is equal to 10!. Thanks . – Sunil Mar 22 '16 at 04:58

3 Answers3

1

Now, also mathematically speaking n! > 2 x (n - 1)! for all n > 2. In other words, there does not exist a pair of numbers from [1..9] such that the sum of the factorials equals 10 factorial. There are no solutions.

The problem with your code is that the break is only hit when you get a solution to the problem.

There are no solutions.

So your code continues the while loop forever, incrementing c1 until the call to findFactorial(c1) causes a stack overflow.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

As discussed in C++ sum of two factorials euals factorial of 10 find two values say x and y whose factorials sums up the only solution in {0,1} pair.

Since search in the sample starts with 1 it can't find solution at all. So while loop never stops. Notice that in expression c1! + i! the c1 max value is not restricted at all (unlike i which belongs to 1-10 range). This leads to computing factorials of bigger and bigger numbers till StackOverflow happens around 14000!.

Fix - start both values with 0 and stop iteration when c1 is 10 or have nested for loops 0-10 instead of while(found).


Note that due to overflow behavior of integer calculations it is ok to try to compute factorial of large numbers like 100! with code shown (also it will return 0 as shown in Why computing factorial of realtively small numbers (34+) returns 0) till you get Stack overflow with recursive code on very large ( about 14000! for me) numbers.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
-4

Your problem is that you're doing this recursively, and that's very intensive on stack space. My advice would be to rewrite so it's just iterative; even though it's cognitively simpler to write it recursively all those method calls actually add up. If it's for an assignment or something and you HAVE to do it recursively, add some stack space.

How to change stack size for a .NET program?

Community
  • 1
  • 1
user1385417
  • 131
  • 5