A boolean circuit in TCS is basically a DAG consisting of And, Or, Not gates, and by what is known is "functional completeness" they can compute all possible functions. eg this is the basic principle in an ALU.

Challenge: create a circuit to determine whether an 8-binary-digit number is divisible by 3 and somehow visualize your result (ie in some kind of picture)

The judging criteria for voters is based on whether the code to generate the circuit generalizes nicely to arbitrary size numbers, and whether the algorithmically-created visualization is compact/balanced but yet still human-readable (ie visualization by hand arrangement not allowed). ie the visualization is for n=8 only but the code will ideally work for all 'n'. winning entry is just top voted.

Much better. However "generalize" and "aesthetic" are not objective. All questions must have an objective winning criterion. If you want to apply those characteristics, use popularity-contest. If you want shortest code, use code-golf. If you want to make a combination of the two, use code-challenge, but specify a formula. For example 1.25*votes - 0.25*length as this question does: codegolf.stackexchange.com/questions/23581/eiffel-tower-in-3d/…
– Level River StMar 25 '14 at 0:30

ok made those chgs you ask for. thx for feedback.
– vznMar 25 '14 at 1:00

Oghh, I guess compiled VHDL or Verilog after all its optimizations should give the shortest answer. I'll try it later.
– Kirill KulakovMar 25 '14 at 6:26

@TheDoctor ??? what is gate-golf? that tag is nonexistent. note to participants: please state what language/visualization tool you are using. if anyone else wants to enter plz comment. otherwise will accept winner tonite. thx much to respondents so far this went "BTE" better than expected!
– vznMar 25 '14 at 14:49

3 Answers
3

The graph maintains 3 booleans at each level i. They represent the fact that the high-order i bits of the number are equal to 0, 1, or 2 mod 3. At each level we compute the next three bits based on the previous three bits and the next input bit.

Here's the python code that generated the graph. Just change N to get a different number of bits, or K to get a different modulus. Run the output of the python program through dot to generate the image.

While there are more than two bits in the number, take two lowest pairs of bits and sum them modulo 3, then append the result to the back of the number, then return if the last pair is zero modulo 3. If there is an odd number of bits in the number, add an extra zero bit to the top, then polish with constant value propagation.

Appending to the back instead of to the front ensures the addition tree is a balanced tree rather than a linked list. This, in turn, ensures logarithmic depth in the number of bits: five gates and three levels for pair cancellation, and an extra gate at the end.

Of course, if approximate planarity is desired, pass the top pair unmodified to the next layer instead of wrapping it to the front. This is easier said than implemented (even in pseudocode), however. If the number of bits in a number is a power of two (as is true in any modern computer system as of March 2014), no lone pairs will occur, however.

If the layouter preserves locality / performs route length minimisation, it should keep the circuit readable.

This Ruby code will generate a circuit diagram for any number of bits (even one). To print, open in Logisim and export as image:

looks really great & best circuit/layout so far. what language is the code in? what layouter did you use if any? the layouter was requested to be an algorithm, and have to assume that algorithm was not used (hand layout) unless stated...
– vznMar 25 '14 at 14:54

@vzn the layouter had to be implemented, too? I understood that restriction as meaning that we could draw the diagram manually, but readability must not depend on the way the diagram is drawn. TimWolla's circuit is definitely hand-generated. The pseudo-code is mostly based on Haskell with Javascripty objects added.
– John DvorakMar 25 '14 at 15:01

algorithmically created visualization was meant to mean basically algorithmic layouter but now admit that could be ambiguously interpreted. sorry for lack of crystal clarity. do you know of any automated layouter that can get nearly similar results to your hand layout?
– vznMar 25 '14 at 15:30

Unfortunately, no. yEd has great layouters but it ignores orientation. I've never got familiar with dot nor I find its output exactly nice. I guess I could translate this pseudocode to Ruby (easy) and write my own specialised layouter (not too hard, but complex) that would export a logisim circuit (it's just an XML, and it's not even gzipped, so quite easy). Should I (I want to), and should I post that as a separate answer? Also, would that count as hand-designed?
– John DvorakMar 25 '14 at 15:46

All good answers, but this looks like the most elegant so far
– Digital TraumaMar 25 '14 at 16:42

wow nice/fast! ... plz put a link to code or inline it ... also detail how you did the visualization ...?
– vznMar 25 '14 at 4:00

@vzn The visualization was made with logisim. It was built my hand, but the general algorithm can easily be done with a program as well. It is partly explained in the answer.
– TimWollaMar 25 '14 at 15:05

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).