2

I'm stuck in a simple question of C++ but this has been tough for me now. Can anybody help me out with this issue.

I have written C++ code for the problem i know that code cannot print out as the question's answer But i don't know how to solve it now.

The code is as below:

#include<iostream.h>
#include<conio.h>

long int* solve10();
void main()
{
   clrscr();
   long int* solveten;
   cout<<"Solve for ten is x factorial + y factorial equals factorial of 10"<<endl;
   solveten=solve10();
   cout<<"The first value is x and the second is y:"<<endl;
   cout<<"The value x and the value y are:"<<endl;
    for(int i=0;i<=1;i++){
            cout<<*(solveten+i)<<endl;
    }
 getch();
}
 long int* solve10(){
   static long int a[2];
   int k=1,x=1,l=1,y;
   long int mul=1;
    for(int i=1;i<=10;i++){
            mul=mul*i;
    }
  do
  {
    k=x*k;
    for(y=1;y<=10;y++){
    l=l*y;
    if((k+l)==mul){
        a[0]=x;
        a[1]=y;

    }

 }
 x++;
 }while(x<=10);
 return a;
}

What will be its best answer??

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
sangham
  • 29
  • 3
  • Since you are using `cout`, this question should not be tagged C. You'll probably find changing your `solve10` function signature to `void solve10 (int& x, int& y)` simpler - just change the x and y values to the correct ones and the caller can print them after the function returns. Other than that, are you having trouble with the actual algorithm, or just printing the values? – IllusiveBrian Aug 05 '14 at 09:33
  • 2
    You need to add comments to your code. Otherwise, we have no way to tell what you expected the code to do. – David Schwartz Aug 05 '14 at 09:33
  • 3
    Take a look at your question. Forget everything you know, just look at your question. If someone else asked the question exactly like you did now, would you understand what's being asked? I sure don't. –  Aug 05 '14 at 09:34
  • In addition to davids and hvds comment, please also use proper indention. It helps us not having to look 5 times to see where a scope ends and it also helps you, if its not a side effect of copying out of your IDE. – Slyps Aug 05 '14 at 09:41

1 Answers1

5

You need to reset l to 1 before entering the for y loop.

But actually, x! + y! = 10! doesn't have a solution (note that 9! + 9! < 10!).

The only integer solutions to the equation x! + y! = z! are x = 0, 1; y = 0, 1; z = 2. I have discovered a truly remarkable proof which this SO answer is too small to contain.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • 2
    +1 for the last sentence. The proof fits in a comment, though: `x! + x! = 2 x!` and `(x + 1)! = (x + 1) x!`. Thus we need `2 >= x + 1`. – dornhege Aug 05 '14 at 11:14