r/ProgrammerTIL • u/roomram • Jul 31 '21
Other TIL of De Morgan's Law by accident
It's a helpful law to shorten by a bit your booleanic expressions.
De Morgan's Law:
Given two statements A, B we can say the following -
(not A) and (not B) = not (A or B)
(not A) or (not B) = not (A and B)
Before the story I need to mention that I have yet to take a proper programming course, I learned through online videos and trial and error.
I was doing some boolean comparisons and my code started getting messy. I was basically doing "not A and not B" and wondered if I could take the not outside and have the same result. I drew a quick truth table and tested "not (A and B)". The result was not the same, but I saw a pattern and something told me to change the "and" to an "or". I did and it was a perfect match. Happy with my discovery I sent this to a friend who actually just finished studying CS in a university and he wrote to me: "Classic De Morgan's" and I was like WHAT?
He told me someone already discovered it a two hundred years ago and was surprised I discovered that by mistake. He knew of it through a course he took related to boolean algebra and said it's a very basic thing. We laughed about it saying that if I were a mathematician in 1800 I would be revolutionary.
73
u/chrismasto Jul 31 '21
I’m surprised Boolean algebra/logic is not (universally) taught in school. I remember a lot of time spent on this. But maybe I remember it because I enjoyed the subject. And it’s fun to say “contrapositive”.
26
u/[deleted] Jul 31 '21
I once asked my math teacher if we could approximate stuff like a sine curve through polynomials... I was 300 years late...
But it's always fun to come up with these things independently, just gives a slightly warm and fuzzy feeling.
10
u/theevildjinn Jul 31 '21
Wolfram is useful for these:
https://www.wolframalpha.com/input/?i=%28not+a%29+and+%28not+b%29
19
u/[deleted] Jul 31 '21
I try to make sure my code is readable. So while you can do this, I try to write my logic so that someone else can read it and make sense of the statement. But who knows, maybe it's more readable if you apply De Morgan's.
13
u/[deleted] Jul 31 '21
I didn't know it had a name. I call that "extract the not and reverse the operator".
6
u/robin_888 Aug 01 '21
I once (re-)discovered Dijkstra's algorithm for finding the shortest paths between nodes in a graph by thinking about finding the shortest way in a grid (of pixels), so I can relate.
Moral: Only because things are named after smart heads doesn't mean they have to be very hard.
2
u/_ologies Jul 31 '21
This feels like the distributive property in Algebra.
1
u/leopardus343 May 12 '22
Good old Captain DeMorgan