Express all 16 boolean functions with the less than operator
There are a few options for some of these, so this 100-char set isn't identical to the previously posted ones.
0
(B<A)<A
B<A
A
A<B
B
((A<B)<((B<A)<1))<1
(A<(B<1))<1
A<(B<1)
(A<B)<((B<A)<1)
B<1
(A<B)<1
A<1
(B<A)<1
((B<A)<A)<1
1
An easier proof that <
is functionally complete would be that A<(B<1)
gives NOR.
The code which I used to find this is a heavy simplification of some Boolean optimisation code I've used on earlier challenges, with two small changes:
- Make the score of an expression be the length of its string rather than the number of operations.
- Make the string avoid unnecessary parentheses, to minimise the length.
import java.util.*;
public class PPCG48193 {
public static void main(String[] args) {
Expr[] optimal = new Expr[16];
int unfound = 16;
PriorityQueue<Expr> q = new PriorityQueue<Expr>();
q.offer(new Expr(0, "0"));
q.offer(new Expr(15, "1"));
q.offer(new Expr(3, "A"));
q.offer(new Expr(5, "B"));
while (unfound > 0) {
Expr e = q.poll();
if (optimal[e.val] != null) continue;
optimal[e.val] = e;
unfound--;
for (Expr e2 : optimal) {
if (e2 != null) {
Expr e3 = e.op(e2), e4 = e2.op(e);
if (optimal[e3.val] == null) q.offer(e3);
if (optimal[e4.val] == null) q.offer(e4);
}
}
}
for (Expr e : optimal) System.out.println(e.expr);
}
private static class Expr implements Comparable<Expr> {
public final int val;
public String expr;
public Expr(int val, String expr) {
this.val = val;
this.expr = expr;
}
public Expr op(Expr e) {
String l = expr.contains("<") ? String.format("(%s)", expr) : expr;
String r = e.expr.contains("<") ? String.format("(%s)", e.expr) : e.expr;
return new Expr((15 - val) & e.val, String.format("%s<%s", l, r));
}
public int compareTo(Expr e) {
int cmp = expr.length() - e.expr.length();
if (cmp == 0) cmp = val - e.val;
return cmp;
}
}
}
100 characters
0
(A<1)<B
B<A
A
A<B
B
((B<A)<((A<B)<1))<1
(A<(B<1))<1
A<(B<1)
(B<A)<((A<B)<1)
B<1
(A<B)<1
A<1
(B<A)<1
((A<1)<B)<1
1
100 characters
0
(A<B)<B
B<A
A
A<B
B
((A<B)<((B<A)<1))<1
(B<(A<1))<1
B<(A<1)
(A<B)<((B<A)<1)
B<1
(A<B)<1
A<1
(B<A)<1
((A<B)<B)<1
1