|
原帖由 lastwinner 于 10-8-6 00:50 发表 ![]()
同怀疑,但我现在找不到证明方法
明天写个sql,用排列组合求和,然后找缺失的数……
遵循该思路,但是,采用distinct level的方法,数据量太大太恐怖了 (参阅 http://www.itpub.net/viewthread. ... p%3Bfilter%3Ddigest)
所以我采用一种变通的方法来尝试此问题
具体思路会在下面的程序中指明
先看表结构:
- CREATE TABLE SQUARENUM
- (
- N NUMBER,
- SN NUMBER
- )
- LOGGING
- NOCOMPRESS
- NOCACHE
- NOPARALLEL
- MONITORING;
- COMMENT ON TABLE SQUARENUM IS '平方数基表';
- CREATE UNIQUE INDEX SQUARENUM_PK ON SQUARENUM
- (N)
- LOGGING
- NOPARALLEL;
- ALTER TABLE SQUARENUM ADD (
- CONSTRAINT SQUARENUM_PK
- PRIMARY KEY
- (N)
- USING INDEX SQUARENUM_PK);
- CREATE TABLE SUMSQUARE
- (
- S NUMBER,
- CONSTRAINT SUMSQUARE_PK
- PRIMARY KEY
- (S)
- USING INDEX SUMSQUARE_PK
- )
- ORGANIZATION INDEX
- LOGGING
- NOPARALLEL
- MONITORING;
- COMMENT ON TABLE SUMSQUARE IS '可拆分为不相同的平方数的和的数';
- CREATE TABLE SUMSQUARE_P
- (
- CURRSN NUMBER
- )
- LOGGING
- NOCOMPRESS
- NOCACHE
- NOPARALLEL
- MONITORING;
- COMMENT ON TABLE SUMSQUARE_P IS '指明当前操作处理到哪个数字了';
复制代码
再看数据初始化脚本:
- insert into SUMSQUARE --将向表中插入前15个平方数任意组合后得到的和,去重后将不同和值以及0插入到表中
- -- 0本质上不算符合条件的数,加入0只是为了便于后面存储过程的操作
- select 0 from dual
- union
- select distinct dbms_aw.eval_number(xmlpath) from (
- select sn, 0||sys_connect_by_path(sn, '+') xmlpath from
- (select * from squarenum where n<=15)
- connect by n<prior n
- )
- order by 1 asc;
- Insert into SUMSQUARE_P (CURRSN) Values (0);
- COMMIT;
复制代码
dbms_aw.eval_number 参阅 http://www.itpub.net/thread-1266657-1-1.html
以及存储过程(这是核心)
- create or replace procedure insertss(st in number, en in number) as
- v_n number:=-1;
- begin
- select currsn into v_n from sumsquare_p;
- v_n :=greatest(st, v_n);
- for v_n in st .. en loop
- insert into SUMSQUARE
- with tt as (select sn from SQUARENUM where n=v_n)
- select distinct (num.sn+total.s) from tt num, SUMSQUARE total
- minus
- select s from SUMSQUARE where s>= (select sn from tt);
- UPDATE SCOTT.SUMSQUARE_P SET CURRSN = v_n;
- end loop;
- end;
- /
复制代码
该存储过程是将当前已有可拆分为不同平方数和的数与更大的一个平方数相加,然后从和中去除在sumsquare中已经存在的值
st和en分别表示开始和结束的平方数的平方根
在初始化完成后,sumsquare表中的数据指标情况如下:
sql> select max(s) maxs, count(*) cnt, max(s)-count(*) gap from sumsquare;
MAXS CNT GAP
---------- ---------- ----------
1240 1179 61
[ 本帖最后由 lastwinner 于 2010-8-6 13:22 编辑 ] |
|