BooleanCountingFunction[kmax,n]
represents a Boolean function of n variables that gives True if at most kmax variables are True.
BooleanCountingFunction[{k},n]
represents a function of n variables that gives True if exactly k variables are True.
BooleanCountingFunction[{kmin,kmax},n]
represents a function that gives True if between kmin and kmax variables are True.
BooleanCountingFunction[{{k1,k2,…}},n]
represents a function that gives True if exactly ki variables are True.
BooleanCountingFunction[spec,{a1,a2,…}]
gives the Boolean expression in variables ai corresponding to the Boolean counting function specified by spec.
BooleanCountingFunction[spec,{a1,a2,…},form]
gives the Boolean expression in the form specified by form.
BooleanCountingFunction
BooleanCountingFunction[kmax,n]
represents a Boolean function of n variables that gives True if at most kmax variables are True.
BooleanCountingFunction[{k},n]
represents a function of n variables that gives True if exactly k variables are True.
BooleanCountingFunction[{kmin,kmax},n]
represents a function that gives True if between kmin and kmax variables are True.
BooleanCountingFunction[{{k1,k2,…}},n]
represents a function that gives True if exactly ki variables are True.
BooleanCountingFunction[spec,{a1,a2,…}]
gives the Boolean expression in variables ai corresponding to the Boolean counting function specified by spec.
BooleanCountingFunction[spec,{a1,a2,…},form]
gives the Boolean expression in the form specified by form.
Details
- BooleanCountingFunction[spec] gives a Boolean function object that works like Function.
- BooleanCountingFunction[spec][a1,a2,…] gives an implicit representation equivalent to the explicit Boolean expression BooleanCountingFunction[spec,{a1,a2,…}].
- BooleanConvert converts BooleanCountingFunction[spec][vars] to an explicit Boolean expression.
- BooleanCountingFunction[{kmin,kmax,s},…] represents a function that gives True if kmin, kmin+s, …, kmax variables are True.
- Any symmetric Boolean function can be represented uniquely using BooleanCountingFunction.
- In BooleanCountingFunction[spec,{a1,a2,…},form], the possible forms are as given for BooleanConvert.
- BooleanCountingFunction[spec,{a1,a2,…}] by default gives an expression in disjunctive normal form (DNF).
Examples
open all close allBasic Examples (1)
Scope (6)
Specify that f is true when at most 2 arguments are true:
f = BooleanCountingFunction[2, 4]{f[True, True, False, False], f[True, True, False, True]}g = BooleanCountingFunction[{2}, 4]{g[True, False, True, False], g[False, True, False, False]}Between 2 and 3 arguments are true:
h = BooleanCountingFunction[{2, 3}, 4]{h[True, False, True, True], h[True, False, False, False]}1, 3, or 5 arguments are true:
i = BooleanCountingFunction[{1, 5, 2}, 5]{i[True, False, True, False, True], i[True, False, False, False, True]}Specify that f is true when exactly 1, 4, or 5 arguments are true:
f = BooleanCountingFunction[{{1, 4, 5}}, 5]{f[False, True, True, True, True], f[False, True, False, False, True]}BooleanCountingFunction is by default preserved in function form:
f = BooleanCountingFunction[{2, 3}, 5][a, b, c, d, e]Use BooleanConvert to convert to other forms:
BooleanConvert[f, "DNF"]BooleanConvert[f, "CNF"]BooleanCountingFunction is automatically converted when given an explicit list of variables:
BooleanCountingFunction[{2, 3}, {a, b, c, d, e}]BooleanCountingFunction[{2, 3}, {a, b, c, d, e}, "CNF"]The expanded forms can be large when the number of variables grows:
Length@BooleanCountingFunction[{5, 15}, Array[a, 20]]The performance gain in evaluating the function form can be substantial:
f = BooleanCountingFunction[{5, 15}, Array[a, 20]];g = BooleanCountingFunction[{5, 15}, 20]@@Array[a, 20];rules = Thread[Array[a, 20] -> RandomChoice[{True, False}, 20]];Timing@(f /. rules)Timing@(g /. rules)Constant arguments are reduced:
BooleanCountingFunction[{3, 4}, 6]@@{a, True, b, False, c, d}BooleanCountingFunction[{3, 4}, 6]@@{a, True, b, True, c, d}Extreme cases are automatically converted to formulas:
BooleanCountingFunction[0, 4]@@{a, b, c, d}BooleanCountingFunction[4, 4]@@{a, b, c, d}BooleanCountingFunction[{4}, 4]@@{a, b, c, d}Applications (4)
Create new primitives that are true when at most, at least, or exactly k arguments are true:
AtMostK[k_, v_] := BooleanCountingFunction[{0, k}, Length[v]]@@vAtLeastK[k_, v_] := BooleanCountingFunction[{k, Length[v]}, Length[v]]@@vExactlyK[k_, v_] := BooleanCountingFunction[{k}, Length[v]]@@vCreate a number of disk regions along the unit circle:
ineqs = Table[(x - Cos[i 2Pi / 6]) ^ 2 + (y - Sin[i 2Pi / 6]) ^ 2 < 1, {i, 0, 5}]Show the newly combined regions:
{RegionPlot[AtMostK[2, ineqs], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 35],
RegionPlot[AtLeastK[2, ineqs], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 35], RegionPlot[ExactlyK[2, ineqs], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 35]}NIntegrate[Boole[AtLeastK[2, ineqs]], {x, -2, 2}, {y, -2, 2}]Integrate[Boole[AtLeastK[2, ineqs]], {x, -2, 2}, {y, -2, 2}]Define a Boolean function that is true when the number of true arguments is k modulo m:
BooleanModCount[{k_, m_}, v_] := BooleanCountingFunction[{Mod[k, m], Length[v], m}, Length[v]]@@vWhen k=0 and m=2, you get Xnor:
TautologyQ[Equivalent[Xnor@@Array[a, 10], BooleanModCount[{0, 2}, Array[a, 10]]]]When k=1 and m=2, you get Xor:
TautologyQ[Equivalent[Xor@@Array[a, 10], BooleanModCount[{1, 2}, Array[a, 10]]]]For other values of k and m, you get new functionality:
Table[BooleanModCount[{1, 3}, Join[ConstantArray[True, {k}], ConstantArray[False, {10 - k}]]], {k, 0, 10}]Boole[%]ArrayPlot[BooleanTable[BooleanModCount[{1, 3}, Array[a, 10]], Array[a, 5], Array[a, 5, 6]], ColorRules -> {False -> White, True -> Black}]Define a Boolean function that sorts a list of truth values:
BooleanSort[v_] := Table[BooleanCountingFunction[{k, Length[v]}, Length[v]]@@v, {k, Length[v], 1, -1}]The resulting list is always in sorted order:
BooleanTable[BooleanSort[{a, b}], {a, b}]BooleanTable[BooleanSort[{a, b, c}], {a, b, c}]Find the mean time to failure for a system that needs two out of three components to work:
{𝒟1, 𝒟2, 𝒟3} = {ExponentialDistribution[Subscript[λ, 1]], ExponentialDistribution[Subscript[λ, 2]], ExponentialDistribution[Subscript[λ, 3]]};ℛ = ReliabilityDistribution[BooleanCountingFunction[{2, 3}, {x, y, z}], {{x, 𝒟1}, {y, 𝒟2}, {z, 𝒟3}}];Mean[ℛ]Properties & Relations (6)
BooleanCountingFunction is symmetric in its arguments:
f = BooleanCountingFunction[{1, 2}, 3]f@@@Permutations[{True, False, True}]Logical combinations of BooleanCountingFunction correspond to set operations on indices:
e1 = BooleanCountingFunction[{{1, 3}}, 4][a, b, c, d]∨BooleanCountingFunction[{{1, 4}}, 4][a, b, c, d];e2 = BooleanCountingFunction[{Union[{1, 3}, {1, 4}]}, 4][a, b, c, d];TautologyQ[Equivalent[e1, e2]]The basic specification can equivalently be specified using Range:
f = BooleanCountingFunction[{1, 5, 2}, 5]g = BooleanCountingFunction[{Range[1, 5, 2]}, 5]TautologyQ[Equivalent[f@@Array[a, 5], g@@Array[a, 5]], Array[a, 5]]Many primitives can be expressed in terms of BooleanCountingFunction:
vars = Array[x, 10];And:
and[v__] := BooleanCountingFunction[{Length[{v}]}, Length[{v}]][v]TautologyQ[Equivalent[And@@vars, and@@vars]]Or:
or[v__] := BooleanCountingFunction[{1, Length[{v}]}, Length[{v}]][v]TautologyQ[Equivalent[Or@@vars, or@@vars]]Nand:
nand[v__] := BooleanCountingFunction[Length[{v}] - 1, Length[{v}]][v]TautologyQ[Equivalent[Nand@@vars, nand@@vars]]Nor:
nor[v__] := BooleanCountingFunction[0, Length[{v}]][v]TautologyQ[Equivalent[Nor@@vars, nor@@vars]]Xor:
xor[v__] := BooleanCountingFunction[{1, Length[{v}], 2}, Length[{v}]][v]TautologyQ[Equivalent[Xor@@vars, xor@@vars]]Xnor:
xnor[v__] := BooleanCountingFunction[{0, Length[{v}], 2}, Length[{v}]][v]TautologyQ[Equivalent[Xnor@@vars, xnor@@vars]]equivalent[v__] := BooleanCountingFunction[{{0, Length[{v}]}}, Length[{v}]][v]TautologyQ[Equivalent[Equivalent@@vars, equivalent@@vars]]majority[v__] := BooleanCountingFunction[{Floor[Length[{v}] / 2] + 1, Length[{v}]}, Length[{v}]][v]TautologyQ[Equivalent[Majority@@vars, majority@@vars]]The size of the truth set for BooleanCountingFunction is the length of Subsets:
SatisfiabilityCount[BooleanCountingFunction[{5, 10}, 15]]Length[Subsets[Range[15], {5, 10}]]The size of the truth set for BooleanCountingFunction can be given by a combinatorial sum:
SatisfiabilityCount[BooleanCountingFunction[{300, 700}, 1000]@@Array[a, 1000]]Sum[Binomial[1000, k], {k, 300, 700}]%% == %Neat Examples (1)
BooleanCountingFunction for when exactly i variables are true has disjoint truth sets:
Table[ArrayPlot[BooleanTable[BooleanCountingFunction[{i}, 10]@@Array[x, 10], Array[x, 5], Array[x, 5, 6]], ColorRules -> {False -> None, True -> ColorData["Rainbow"][i / 11]}], {i, 0, 10}]Show[%]Related Guides
History
Text
Wolfram Research (2008), BooleanCountingFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/BooleanCountingFunction.html.
CMS
Wolfram Language. 2008. "BooleanCountingFunction." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/BooleanCountingFunction.html.
APA
Wolfram Language. (2008). BooleanCountingFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BooleanCountingFunction.html
BibTeX
@misc{reference.wolfram_2026_booleancountingfunction, author="Wolfram Research", title="{BooleanCountingFunction}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/BooleanCountingFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_booleancountingfunction, organization={Wolfram Research}, title={BooleanCountingFunction}, year={2008}, url={https://reference.wolfram.com/language/ref/BooleanCountingFunction.html}, note=[Accessed: 13-June-2026]}