I believe you only care about odd number of inputs, because even number of inputs doesn't really make sense. Like what is the majority of 2 zeros and 2 ones? I don't know the answer to that. But I do know the majority of 2 zeros and 3 ones, in other words, odd number of inputs.
So let's calculate two majority equations with 3 and 5 inputs and see if we can find any pattern.
With 3 inputs, using Karnaugh maps, you will get this answer if you try to make them all into NAND's:
\$M = \overline{\overline{AB}~\overline{AC}~\overline{BC}}\$
A,B,C are each used 2 times to form all combinations.
The number of grouped NANDs at the input is \${3 \choose 2} = 3\$
With 5 inputs, using Karnaugh maps, you will get this answer:
\$M = \overline{\overline{ABC}~\overline{ABD}~\overline{ACD}~\overline{BCD}~\overline{CDE}~\overline{ABE}~\overline{ACE}~\overline{BCE}~\overline{ADE}~\overline{BDE}}\$
A,B,C,D,E are each used 6 times to form all combinations.
The number of grouped NANDs at the input is \${5 \choose 3} = 10\$
I think I see the pattern and can extrapolate it to get this equation:
- N = number of inputs (should be odd)
- Total number of NAND-gates \$ = {N \choose \lceil \frac{N}{2} \rceil }+1\$
So with, say 11 inputs you will have \${11 \choose 6}+1 = 463\$ NAND gates, 462 of them will have 6 inputs each, and the 463rd will have 462 inputs.
I haven't made a 7 input majority input, so I can't verify the equation above through induction, but my make-a-sense-o-meter says that it makes sense.
I don't know any good algorithm for mixing \$\lceil\frac{N}{2}\rceil\$, which is essentially AB, AC, BC in the first equation at the top. My first attempt would be to just do it with a couple of nested for loops in c++/matlab/octave and call it a day.