Last week, I closed with a puzzle: "Invert three inputs using only two NOT gates." I should start off by pointing out that I cannot take credit for this problem; I believe it was first penned by Clive Maxfield just over two years ago. Check out his page on it for a full discussion. That said, I will take credit for extending it to the n case, if you really insist. The solutions that follow are just two of many possible ways of looking at this. Structurally, they are by no means the simplest solutions, but I find them to be conceptually the simplest. If you haven't tried to solve the puzzle yet I heartily recommend it; it makes knowing the solution so much more rewarding if you have. My solution to this puzzle involves reasoning on the number of inputs with value '1'. If none of the inputs are 1s, then all the outputs must be, and vice versa. To do this, we create a set of temporary variables with the goal of knowing exactly how many inputs were 1. 3 := A ∧ B ∧ C From here we can just build our outputs directly: X = 0 ∨ (1 ∧ (B ∨ C)) ∨ (2 ∧ (B ∧ C)) This proof sketch is for odd n. If n is even, just invert the last input directly with a NOT, and use this pattern for the first n-1. As before, we are going to reason on how many inputs (I1 to In) were 1. For this we will use a bit more syntax: {a-b} means anywhere from a to b inputs (inclusive) were 1, and {a, b} means either a or b inputs were 1, with no other possibilities allowed. First, we can generate all the ranges ending in n directly: {1-n} := (I1 ∨ I2 ∨ … ∨ In) Clearly, this doesn't need any NOTs. Next, we are going to make all the ranges starting in 0 and ending in an odd number: {0-1} := ¬ {2-n} This requires (n-1)/2 NOT gates. Using these together, we can have variables for all the odd numbers of inputs directly: 1 := {0-1} ∧ {1-n} Again, this doesn't require any NOTs. Lastly, we use one more NOT to let us get all the even values: {0,2,4,…,(n-1)} := ¬ (1 ∨ 3 ∨ … ∨ n) There! Now we have a variable for each possible number of inputs with value 1, so we can just plug in all possibilities to get our outputs: O1 = 0 ∨ (1 ∧ (I2 ∨ I3 ∨ … ∨ In)) ∨ … ∨ (n-1 ∧ (I2 ∧ I3 ∧ … ∧ In)) As you have seen, this took a total of (n-1)/2 + 1 NOT gates. Since n is odd, (n-1)/2 + 1 = ⌊n/2⌋ + 1, and we're done! Whew, I wouldn't want to implement that.Solution for 3 inputs
2or3 := (A ∧ B) ∨ (B ∧ C) ∨ (A ∧ C)
0or1 := ¬ 2or3
1 := 0or1 ∧ (A ∨ B ∨ C)
0or2 := ¬ (1 ∨ 3)
0 := 0or1 ∧ 0or2
2 := 0or2 ∧ 2or3
Y = 0 ∨ (1 ∧ (A ∨ C)) ∨ (2 ∧ (A ∧ C))
Z = 0 ∨ (1 ∧ (A ∨ B)) ∨ (2 ∧ (A ∧ B))Solution for n inputs
{2-n} := ((I1 ∧ I2) ∨ (I1 ∧ I3) ∨ … ∨ (In-1 ∧ In))
…
{n-n} := (I1 ∧ I2 ∧ … ∧ In)
{0-3} := ¬ {4-n}
…
{0-(n-2)} := ¬{(n-1)-n}
3 := {0-3} ∧ {3-n}
…
n-2 := {0-(n-2)} ∧ {(n-2)-n}
n := {n-n}
0 := {0-1} ∧ {0,2,4,…,(n-1)}
2 := {0-3} ∧ {0,2,4,…,(n-1)} ∧ {2-n}
4 := {0-5} ∧ {0,2,4,…,(n-1)} ∧ {4-n}
…
n-1 := {0,2,4,…,(n-1)} ∧ {(n-1)-n}
O2 = 0 ∨ (1 ∧ (I1 ∨ I3 ∨ … ∨ In)) ∨ … ∨ (n-1 ∧ (I1 ∧ I3 ∧ … ∧ In))
…
On = 0 ∨ (1 ∧ (I1 ∨ I2 ∨ … ∨ In-1)) ∨ … ∨ (n-1 ∧ (I1 ∧ I1 ∧ … ∧ In-1))
Thursday, May 29, 2008
NOT Puzzle Solution
blog comments powered by Disqus
Subscribe to:
Post Comments (Atom)
I'm Eric Burnett, a software engineer at Google. I'm interested in studying what makes programmers tick, and writing whatever code strikes my fancy. Read my 