# Can we write a program that can say if any 2 given programs do the same w.r.t input - output pairs

I'm new to theoretical CS research. I have the following question: Given 2 different computer programs, each generating certain outputs for a given set of inputs. Assuming we are given the range of values for input variables (i.e., min to max values), is it possible to check with another program whether these 2 programs will give the same output values for all possible input values, without actually running the 2 programs for all input values?

Thanks.

• Are you saying there are only finitely many input? – Raphael Jun 7 '16 at 7:05
• I assume inputs can be of integer or real data types. They can be either within (min, max) range or extend up to infinity. There is no other constraint. – musigma Jun 7 '16 at 7:11
• Stop posting comments as answers. You can always comment on your own posts, and answers to yourt questions. However, you seem to have created multiple accounts; see here on how to remedy that. – Raphael Jun 7 '16 at 7:15

No. That's undecidable. Suppose the first program is

return 42


and the second program is

if (f(x) halts)
return 0
else
return 42


where x is the input.

Do these two programs yield the same output, for all possible inputs? That depends whether f always halts on all inputs. And that is exactly the halting problem, which we know is not decidable. It's easy to see that, as a consequence, this problem can't be decidable either (if it were, you could decide the halting problem).

• Thank you very much. So, we can say that just because 2 programs give same outputs for identical set of input values, we can't say that they will give same output for another input value. My question is related to the way Online Judges like SPOJ and CodeChef test programs submitted by coders. I assume that they run the submitted program on a specific set of inputs. If it passes then they assume the program is correct. Although it may be true most of the time, it is probably not a fool-proof method. Am I right? Thanks. – user53263 Jun 7 '16 at 6:54
• What is f? I guess you want to formulate a decision problem ranging over all f? Note that the OP seems to restrict the problem to a finite range of inputs. – Raphael Jun 7 '16 at 7:07
• @musigma The general problem is undecidable. That does however not mean that you can not write a checker for some problems/algorithms. – Raphael Jun 7 '16 at 7:08
• @Raphael, well, this is a reduction: f is the instance of the Halting problem. Does the finite range of inputs make the Halting problem decidable? – D.W. Jun 7 '16 at 7:11
• @D.W. I don't think so, but one may have to adapt the reduction. – Raphael Jun 7 '16 at 7:13