I am interested in the following problem:
What is the minimum number of distinct characters used in any programming language that can implement Boolean Algebra?
In particular, what is the set that has the least number of distinct characters used in a programming language of your choosing that satisfies the following:
- The characters must be able to construct a valid program in the chosen language, following the latter restrictions. In other words, all errors (compile time and runtime) ARE NOT ALLOWED. The program is allowed to have unlimited length. Warnings are also allowed.
- Can assign values to the constant
True
andFalse
. Equivalent formulations such as0
and1
,true
andfalse
, or"true"
and"false"
is allowed as long as it satisfies theNAND
formulation (see below) - Can define functions with or without parameters
- Can implement the
NAND
binary operator. That is, the program must be able to define a functionNAND(a, b)
that takes two valuesa
andb
and returnFalse
if both areTrue
, andTrue
otherwise. Note that equivalent formulations are allowed, so you may usef(a, b)
or similar strategies to reduce the character count. Also note that you can implement this function in any way possible. - Lowercase and uppcase letters are count seperately. That means
AaAa
counts as2
distinct characters, butaaaa
as 1. - I/O is not required.
For example, your program should be able to implement the following Python code:
a = True
b = False
def NAND(a: bool, b: bool) -> bool:
# note that any equivalent way to implement this is OK, this is one example. Another method is `return False if (a == True and b == True) else True`
return not(a, b)
print(NAND(a, b))
With that, what is the answer for your programming language?
Note: The reason I chose NAND
is because NAND
is able to construct all other operators (AND
, NOT
, OR
, etc.) If you don't like the NAND
, feel free to construct other logical operators instead, as long as the expressions can be combined to create a formula equivalent to a NAND b
for some a
and b
update: added an example pseudocode for clarity
update 2 (by doubleunary
): The number of distinct characters is the output of the following Python code, replacing the placeholder with your code:
your_code = """
<your_code_here>
"""
print(len(set(your_code)))
For example, my example above has 28ドル$ distinct characters.
8 Answers 8
Google Sheets / Excel, 6 distinct characters
=1-A1*B1
Characters: {=, 1, -, A, *, B}
In GS/Excel AND(a,b)
is logically equivalent to a*b
and NOT(x)
is logically equivalent to 1-x
.
So NOT(AND(a,b))
is logically equivalent to NOT(a*b)
which is logically equivalent to 1-a*b
. (This is assuming a
, b
are either booleans, 1s or 0s)
-
1\$\begingroup\$ I think you need to also include 0 to encode the false literal in an input cell, if I understand the challenge correctly. \$\endgroup\$Martin Modrák– Martin Modrák2025年05月28日 14:32:22 +00:00Commented May 28 at 14:32
-
2\$\begingroup\$ @MartinModrák
0
===1-1
. Further, in Google Sheets, a blank value is treated the same as zero in math contexts. \$\endgroup\$doubleunary– doubleunary2025年06月01日 17:18:28 +00:00Commented Jun 1 at 17:18
PowerShell, 13 distinct characters
$o={!$args[!!$a]-or!$args[!$a]}
a nand b
is the same as !(a and b)
, which is the same as !a or !b
; implementing it using or
avoids the need for round brackets before applying the !
operator after and
(PS has no native -nand
), and it also has less distinct characters than and
.
$o = {...}
defines an (unnamed) function and saves it in the variable $o
(o
because that character is already required for the operator -or
).
Any positional parameters passed to the function will end up in the default array $args
.
!!$a
will evaluate to 0
: $a
starts out as $null
, negating it will turn it to $true
, negating it again will turn it $false
, and using it as array index will turn it to 0.
Accordingly, !$a
will evaluate to 1
.
JavaScript, 6 distinct characters
In JavaScript, only []()!+
are required because JSF*ck.
[][(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+!+[]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+!+[]+[!+[]+!+[]])+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]])()([][(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]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!![]+[])[+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[+[]]+(![]+[])[+[]]+(!![]+[])[+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]]+(!![]+[])[+[]]+[+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]]+[+!+[]])[(![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+[+!+[]]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+!+[]+[+!+[]])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]]((!![]+[])[+[]])[([][(!![]+[])[!+[]+!+[]+!+[]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([![]]+[][[]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]](([][(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]]+![]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])()[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])+[])[+!+[]])+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]])())
console.log(a(0,0));
console.log(a(0,1));
console.log(a(1,0));
console.log(a(1,1));
APL+WIN, 12 distinct characters
NAND is a single character that takes a left and right argument which can be matching boolean arrays of any dimension. The following code prompts for a and b and outputs the NAND result. Try it online shows it for scalar, vector and 2d matrix inputs.
⎕⍲⎕
Desmos, 8 distinct characters
Characters
a_(),=1-
False literal
Assigns a
to 0
a=1-1
True literal
Assigns a_1
to 1
a_1=1
NAND function
Defines a_a
as the NAND function
a_a(a,a_1)=1-aa_1
(Note: the function parameters a
and a_1
are distinct from the variables defined as false and true earlier. Despite that, the program still works completely fine.)
Explanation:
a_(),=1-
a_
: Need for naming (variables, functions, and function parameters)
(),
: Needed for function definition
=
: Assignment operator
1
: Used as both the constant 1, and as a subscript for naming
-
: Used to get 0 from 1, and in the NAND function definition
-
\$\begingroup\$ you can use
a1
anda11
as variable/function names, removing the need for_
\$\endgroup\$corvus_192– corvus_1922025年06月08日 17:55:08 +00:00Commented Jun 8 at 17:55 -
\$\begingroup\$ @corvus_192 That does not work since Desmos is scored by the code that is being pasted into the graphing calculator, not code that is typed into the calculator. If you try to paste in
a1
anda11
verbatim into the graphing calculator, it will not subscript like you likely expect it to. So if you try to paste in code likea1=1-1
ora11=1
, it will simply give an error. You can see the results of that here. \$\endgroup\$Aiden Chow– Aiden Chow2025年06月08日 22:33:10 +00:00Commented Jun 8 at 22:33 -
\$\begingroup\$ I see, it's a peculiarity of the desmos scoring. thanks for the explanation. \$\endgroup\$corvus_192– corvus_1922025年06月09日 14:54:25 +00:00Commented Jun 9 at 14:54
Don't know if my answer is valid. Here we go: 2 distinct chars ! and |.
Tcl, 27 bytes
proc N {a b} {expr !$a|!$b}
Python 3, 11 distinct characters
Characters used (note the space character \u20
and the start of the code block):
*,-1:abdlm
Solution:
lambda a,aa:1-a*aa
Usage in the terminal (two examples):
>>> for i in [True, False]:
... for j in [True, False]:
... print( (lambda a,aa:1-a*aa)(i, j) )
...
0
1
1
1
>>> for i in [True, False]:
... for j in [True, False]:
... print( bool( (lambda a,aa:1-a*aa)(i, j) ) )
...
False
True
True
True
The reason this works is because in Python,
True * True == 1
True * False == 0
False * True == 0
False * False == 0
1 == int(True)
0 == int(False)
R, 6 distinct characters
F=\()&
F
is the built-in false literal and reused in function/param names=
is an assignment operator (which is also used to define named functions). Also let's us build NOT as==F
\
defines an anonymous function()
function call&
logical AND
the NAND definition is then:
FF=\(FF)\(FFF)((FF&FFF)==F)
We can the define the true literal as
FFF=FF(F)(F)
The full NAND truth table is then
FF(F)(F)
#TRUE
FF(F)(FFF)
#TRUE
FF(FFF)(F)
#TRUE
FF(FFF)(FFF)
#FALSE
If you want to define multiple functions or evaluate multiple expressions or define the function AND evaluate an expression you also need a single separator character (not counted above), either ;
or newline will work.
lambd,not01=:
? \$\endgroup\$a NAND b
witha
andb
as the true/false values should suffice. \$\endgroup\$