Click here to Skip to main content
15,921,884 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionGood way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 0:00
David198718-Mar-11 0:00 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
CPallini18-Mar-11 1:29
mveCPallini18-Mar-11 1:29 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 1:41
David198718-Mar-11 1:41 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 2:18
sitebuilderLuc Pattyn18-Mar-11 2:18 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 2:24
David198718-Mar-11 2:24 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 3:32
sitebuilderLuc Pattyn18-Mar-11 3:32 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 3:47
David198718-Mar-11 3:47 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 3:02
_Erik_18-Mar-11 3:02 
Depending on how these expressions are (they do not make different computations depending on the entry value), and if the inputs are always natural numbers you can take advantage of the induction method for mathematical demonstrations, so you would only have to check equality for: one base case (input = 0 for example); one random case (say input = n, being n different form the base case); and the next case to the random case (input = n + 1). If these three cases macth, the expressons are equivalent. For example:

We want to check if these two expressions are equivalent for every natural numbers bigger than 1:

- Expression 1: 1+2+3+4+...+n
- Expression 2: (1+n)*n/2

So our base case would be n=1:

-Expression 1: result = 1;
-Expression 2: (1+1)*1/2 = 1;

Our base cases match.

Lets take a random n... lets say 7:

-Expression 1: 1+2+3+4+5+6+7=28;
-Expression 2: (1+7)*7/2 = 28;

Our random cases match.

Lets check now for random+1, I mean, n = 8:

-Expression 1: 1+2+3+4+5+6+7+8=36;
-Expression 2: (1+8)*8/2 = 36;

Random + 1 matches as well, so both expressions are equivalent for every natural number greater or equal to 1.

I think making a general expression comparer with delegates to the methods implementing each expression would not be too difficult.
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 3:18
David198718-Mar-11 3:18 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 3:46
_Erik_18-Mar-11 3:46 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 3:49
David198718-Mar-11 3:49 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 3:59
_Erik_18-Mar-11 3:59 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 4:13
David198718-Mar-11 4:13 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 4:40
_Erik_18-Mar-11 4:40 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 4:52
David198718-Mar-11 4:52 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 5:18
_Erik_18-Mar-11 5:18 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 5:48
sitebuilderLuc Pattyn18-Mar-11 5:48 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 6:21
_Erik_18-Mar-11 6:21 
AnswerRe: Good way to detect a useful portion of equivalent expressions Pin
Luc Pattyn18-Mar-11 7:09
sitebuilderLuc Pattyn18-Mar-11 7:09 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_21-Mar-11 2:55
_Erik_21-Mar-11 2:55 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
molesworth18-Mar-11 4:40
molesworth18-Mar-11 4:40 
GeneralRe: Good way to detect a useful portion of equivalent expressions [modified] Pin
_Erik_18-Mar-11 4:44
_Erik_18-Mar-11 4:44 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
David198718-Mar-11 4:54
David198718-Mar-11 4:54 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
_Erik_18-Mar-11 5:24
_Erik_18-Mar-11 5:24 
GeneralRe: Good way to detect a useful portion of equivalent expressions Pin
Matty221-Apr-11 1:51
Matty221-Apr-11 1:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.