|
与上面算法基本等价的sql:
with t as (select column_value p from table(pkg_prim2.seive_numlist_nk(100000)))
,tpower as (select /*+ MATERIALIZE */ p, k, power(p,k) n, (p-1)*power(p,k-1) phi from t, (select rownum k from dual connect by rownum<=log(2,100000))
where power(p,k) <= 100000
union select 1,1,1,1 from dual)
,t2 as (select
t1.n n1, t2.n n2, t3.n n3, t4.n n4, t5.n n5, t6.n n6, t7.n n7,
t1.n*t2.n*t3.n*t4.n*t5.n*t6.n*t7.n n, t1.phi*t2.phi*t3.phi*t4.phi*t5.phi*t6.phi*t7.phi phi
from tpower t1, tpower t2, tpower t3,tpower t4, tpower t5, tpower t6, tpower t7
where t1.n>t2.n
and t2.n<=100000/t1.n
and t3.n<=100000/(t1.n*t2.n)
and t4.n<=100000/(t1.n*t2.n*t3.n)
and t5.n<=100000/(t1.n*t2.n*t3.n*t4.n)
and t6.n<=100000/(t1.n*t2.n*t3.n*t4.n*t5.n)
and t7.n<=100000/(t1.n*t2.n*t3.n*t4.n*t5.n*t6.n)
and (t2.n>t3.n or (t2.n=1 and t3.n=1))
and (t3.n>t4.n or (t3.n=1 and t4.n=1))
and (t4.n>t5.n or (t4.n=1 and t5.n=1))
and (t5.n>t6.n or (t5.n=1 and t6.n=1))
and (t6.n>t7.n or (t6.n=1 and t7.n=1))
)
,t3 as (select n, max(phi) phi from t2 group by n)
--select * from t3
select count(*), sum(phi) from t3
COUNT(*) SUM(PHI)
---------- ----------
99999 3039650753
Executed in 57.188 seconds
从算法上看不出与上面的plsql有什么区别, 但这个计算到100000就要近1分钟了, 不知问题出在哪儿。 |
|