Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify logic statement

I have this:

if(!A or (A and B))   //pseudocode

I want to negate that if-statement:

This should work:

if(!(!A or (A and B)))  //pseudocode

but I believe there is a way to simplify it and it's escaping me at the moment.

like image 892
Alexander Mills Avatar asked Dec 25 '22 11:12

Alexander Mills


2 Answers

Welcome to the world of de-Morgan for boolean algebra followed by simple distribution :

if(!(!A or (A and B)) 
=> if(!(!A) and !(A and B))
=> if(A and (!A or !B))
=> if((A and !A) or (A and !B))
=> if(A and !B) 
like image 140
Naman Avatar answered Feb 05 '23 09:02

Naman


If you break it down to a truth table...

A B  !A  (A and B)  A! or (A and B)
0 0  1   0          1
0 1  1   0          1
1 0  0   0          0
1 1  0   1          1

You can see the result is true in all cases except A and !B without having to know/remember any Boolean algebra rules. "Except" is just "not" so... !(A and !B)...

However if writing !A or (A and B) aligns better to the real-world problem you're trying to code leave it like that, unless it's insanely performance critical...

like image 22
Adam Avatar answered Feb 05 '23 09:02

Adam