トップページに戻る    次のSQLパズルへ    前のSQLパズルへ

11-4 因子の部屋

SQLパズル

■各マスに、1から9までの数字を1つずつ入れます。
■タテ列、ヨコ列のどれにも1から9までの数字が1つずつ入ります。
■マスの左上の数字は、太線で囲まれた部屋に入る数字をすべてかけあわせた値(積)になります。

因子の部屋


SQL

col Aa for 99
col Ba for 99
col Ca for 99
col Da for 99
col Ea for 99
col Ab for 99
col Bb for 99
col Cb for 99
col Db for 99
col Eb for 99
col Ac for 99
col Bc for 99
col Cc for 99
col Dc for 99
col Ec for 99
col Ad for 99
col Bd for 99
col Cd for 99
col Dd for 99
col Ed for 99
col Ae for 99
col Be for 99
col Ce for 99
col De for 99
col Ee for 99

with WorkView as
(select 1 as Val from dual union select 2 from dual union select 3 from dual
union select 4 from dual union select 5 from dual union select 6 from dual union select 8 from dual)
select Aa,Ba,Ca,Da,Ea,
       Ab,Bb,Cb,Db,Eb,
       Ac,Bc,Cc,Dc,Ec,
       Ad,Bd,Cd,Dd,Ed,
       Ae,Be,Ce,De,Ee
from
(select a.Val as Aa,f.Val as Ba,k.Val as Ca,p.Val as Da,u.Val as Ea,
        b.Val as Ab,g.Val as Bb,l.Val as Cb,q.Val as Db,v.Val as Eb,
        c.Val as Ac,h.Val as Bc,m.Val as Cc,r.Val as Dc,w.Val as Ec,
        d.Val as Ad,i.Val as Bd,n.Val as Cd,s.Val as Dd,x.Val as Ed,
        e.Val as Ae,j.Val as Be,o.Val as Ce,t.Val as De,y.Val as Ee
from WorkView a,WorkView b,WorkView c,WorkView d,WorkView e,WorkView f,WorkView g,WorkView h,
WorkView i,WorkView j,WorkView k,WorkView l,WorkView m,WorkView n,WorkView o,WorkView p,
WorkView q,WorkView r,WorkView s,WorkView t,WorkView u,WorkView v,WorkView w,WorkView x,WorkView y)
where Aa=5 and Ab*Ac*Ad=8 and Ae*Be*Ce*De=120 and Ba*Bb=15 and Bc*Bd=2 and Ca*Da=2
  and Cb*Db=4 and Cc=4 and Cd*Dd=15 and Dc*Ec=15 and Ea*Eb=12 and Ed*Ee=2
  and Aa not in(Ab,Ac,Ad,Ae) and Ab not in(Ac,Ad,Ae) and Ac not in(Ad,Ae) and Ad!=Ae
  and Ba not in(Bb,Bc,Bd,Be) and Bb not in(Bc,Bd,Be) and Bc not in(Bd,Be) and Bd!=Be
  and Ca not in(Cb,Cc,Cd,Ce) and Cb not in(Cc,Cd,Ce) and Cc not in(Cd,Ce) and Cd!=Ce
  and Da not in(Db,Dc,Dd,De) and Db not in(Dc,Dd,De) and Dc not in(Dd,De) and Dd!=De
  and Ea not in(Eb,Ec,Ed,Ee) and Eb not in(Ec,Ed,Ee) and Ec not in(Ed,Ee) and Ed!=Ee
  and aA not in(bA,cA,dA,eA) and bA not in(cA,dA,eA) and cA not in(dA,eA) and dA!=eA
  and aB not in(bB,cB,dB,eB) and bB not in(cB,dB,eB) and cB not in(dB,eB) and dB!=eB
  and aC not in(bC,cC,dC,eC) and bC not in(cC,dC,eC) and cC not in(dC,eC) and dC!=eC
  and aD not in(bD,cD,dD,eD) and bD not in(cD,dD,eD) and cD not in(dD,eD) and dD!=eD
  and aE not in(bE,cE,dE,eE) and bE not in(cE,dE,eE) and cE not in(dE,eE) and dE!=eE;


解説

2,4,5,8,15,120の約数(9以下の自然数のみ)の和集合は、
{1,2,3,4,5,6,8}で、
この7つの数の全ての組み合わせを求めて、
where句で積をチェックしてます。