利用DuckDB SQL求解集合数学题
已知集合I={1,2,3,4,5,6},A={(s,t)|s ∈I,t∈I},若B包含于A,且对任意的(a,b)∈B,(x,y)∈B,均有(a-x)(b-y)<=0,则集合B中元素个数的最大值是几?
set variable n=6;
with recursive i as(select i from range(1,getvariable('n')+1)t(i)),
a as(select 1::bigint<<((i1.i-1)*getvariable('n')+i2.i)m, i1.i s, i2.i t from i i1,i i2),
b as(select 1 lv,m,[(a,b),(x,y)]st from(select a.m+a1.m m,a.s a,a.t b,a1.s x,a1.t y from a,a a1 where a.m<a1.m) where (a-x)*(b-y)<=0
union all
select lv+1,a.m+b.m,st +[(a.s,a.t)] from b,a where a.m>b.m /*and a.m & b.m=0*/ and not exists(select 1 from range(1,lv+2) un(nb) where (a.s-b.st[nb][1])*(a.t-b.st[nb][2])>0))
from b where lv=(select max(lv) from b);
求出的最大元素个数是11,一共有252个不同的集合满足要求。本来是需要用对二进制位与运算为0来判断的,但是由于低位全1也比高位1低位0的二进制数小,所以有了a.m>b.m,注释掉与运算判断也不影响结果。
也可以不用range笛卡尔积,而用unnest函数来实现,不过慢一点
set variable n=6;
with recursive i as(select i from range(1,getvariable('n')+1)t(i)),
a as(select 1::bigint<<((i1.i-1)*getvariable('n')+i2.i)m, i1.i s, i2.i t from i i1,i i2),
b as(select 1 lv,m,[(a,b),(x,y)]st from(select a.m+a1.m m,a.s a,a.t b,a1.s x,a1.t y from a,a a1 where a.m<a1.m) where (a-x)*(b-y)<=0
union all
select lv+1,a.m+b.m,st +[(a.s,a.t)] from b,a where a.m>b.m and not exists(select 1 from unnest(b.st) un(nb) where (a.s-nb[1])*(a.t-nb[2])>0))
from b where lv=(select max(lv) from b);
比起python程序,SQL还是简练一点。