During my assignments for theoretical CS I stumbled over the questions how a boolean function would look, that tests if of n given boolean vars, x1 to xn, k or less variables are true.
In Java this would be fairly simple :
public boolean k_or_less_true(boolean[] x , int k) {
int num_true = 0;
int n = boolean.size;
for(int i = 0; i < n-1; i++){
if(x[i]){num_true++;}
}
return num_true <= k;
}
The problem now is to find a formula by means of propositional calculus dependent on n and k, that returns true only and only if k or less of the n given are true.
To give an example, if we let k = 1, the formula corresponds to the NAND-function :
(x1 , x2)<=1 = NOT(x1 AND x2)
(x1 , x2 , x3)<=1 = NOT( (NOT(x1 AND x2)) AND x3)
.
.
.
So far the question is, how the formula changes if k increases...
Also a pretty obvious connection is :
(x1, x2, ..., xn)<=k = (x1, ..., xn)=k OR (x1, ..., xn)=k-1 OR ... OR (x1, ..., xn)<=1