Inverting the universe with two inverters

Recently I came across an interesting puzzle from MIT Tech Review March/April 2013:

Howard Cohen has plenty of AND and OR gates but just two inverters. How can he invert three signals a, b, and c?

More generally, he wonders if the ratio I/S can ever be less than 2/3, where I is the number of inverters and S is the number of signals to invert (once again, unlimited AND and OR gates are available).

I couldn’t quickly solve the first part of the puzzle with a pen and paper, so I decided to write a brute force solver. And to make it a bit more fun I did it in Haskell (any suggestions for improving the code are welcome, by the way!).

Continue reading Inverting the universe with two inverters

An infinite loop bug

It’s the coolest infinite loop I had ever fallen into:

for(char c = 0; c <= 127; c++) {...}

I wonder how I’m supposed to perform the intended iterations without obfuscating the code. Sure, I can just drop char and use int instead, but this does not solve the problem in general.

P.S.: There seem to be no syntax primitive to safely iterate over an interval [L; U] in languages from the C/Java family. This means that you just cannot write code for(T c = L; c <= U; c++) {...} without making assumptions on T and U.

P.P.S.: Pascal rules! Code for c:= 0 to 255 do begin ... end, where c is a byte works just fine 🙂