利用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还是简练一点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值