[Wellington-pm] SQL solution!

Sam Vilain sam at vilain.net
Thu Mar 3 18:31:43 PST 2005


Ewen McNeill wrote:
> quantum physics, by not respecting entanglement.  But I suspect at this
> point they probably care more about compatibility with Perl 6 (which is
> more set theory based I believe).

This made me realise how similar the tasks of collapsing eigenstates into
results is to a set theory expression.

So, here's a solution.  It seems this one takes the cake for execution
time and thoroughness :-)

-- MySQL table setup:

CREATE TABLE Dx ( X INTEGER(1) );
INSERT INTO Dx (X) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE Cx ( X INTEGER(1) );
INSERT INTO Cx (X) VALUES (0),(1);

-- Oracle table setup:

CREATE TABLE Cx ( X NUMBER(1));
CREATE TABLE Dx ( X NUMBER(1));
INSERT INTO Cx (X) VALUES (0);
INSERT INTO Cx (X) VALUES (1);
INSERT INTO Dx (SELECT ROWNUM-1 FROM Cx C0, Cx C1, Cx C2,
                 Cx C3 WHERE ROWNUM <= 10);

-- actual query:

SELECT
     (S.X * 1000 + E.X * 100 + N.X * 10 + D.X) as SEND,
     (M.X * 1000 + Oh.X * 100 + R.X * 10 + E.X) as MORE,
     (M.X * 10000 + Oh.X * 1000 + N.X * 100 + E.X * 10 + Y.X) as MONEY
from
     Dx M
     LEFT JOIN Dx S ON (M.X != S.X)
     LEFT JOIN Dx E ON ( (M.X != E.X) AND (S.X != E.X) )
     LEFT JOIN Dx Oh ON ( (M.X !=Oh.X) AND (S.X !=Oh.X) AND (E.X !=Oh.X) )
     LEFT JOIN Dx N ON ( (M.X != N.X) AND (S.X != N.X) AND (E.X != N.X)
		    AND (Oh.X != N.X) )
     LEFT JOIN Dx R ON ( (M.X != R.X) AND (S.X != R.X) AND (E.X != R.X)
		    AND (Oh.X != R.X) AND (N.X != R.X) )
     LEFT JOIN Dx D ON ( (M.X != D.X) AND (S.X != D.X) AND (E.X != D.X)
		    AND (Oh.X != D.X) AND (N.X != D.X) AND (R.X != D.X) )
     LEFT JOIN Dx Y ON ( (M.X != Y.X) AND (S.X != Y.X) AND (E.X != Y.X)
		    AND (Oh.X != Y.X) AND (N.X != Y.X) AND (R.X != Y.X)
		    AND (D.X != Y.X) )
     LEFT JOIN Cx C0 ON ( C0.X = floor( (D.X + E.X) / 10 ) )
     LEFT JOIN Cx C1 ON ( C1.X = floor( (N.X + R.X + C0.X) / 10 ) )
     LEFT JOIN Cx C2 ON ( C2.X = floor( (E.X + Oh.X + C1.X) / 10 ) )
     LEFT JOIN Cx C3 ON ( C3.X = floor( (S.X + M.X + C2.X) / 10 ) )
WHERE
     ( M.X != 0 ) AND
     ( S.X != 0 ) AND
     (                C3.X    ) = M.X  AND
     MOD( S.X + M.X + C2.X, 10) = Oh.X  AND
     MOD( E.X +Oh.X + C1.X, 10) = N.X  AND
     MOD( N.X + R.X + C0.X, 10) = E.X   AND
     MOD( D.X + E.X       , 10) = Y.X
;



-- 
Sam Vilain, sam /\T vilain |><>T net, PGP key ID: 0x05B52F13
(include my PGP key ID in personal replies to avoid spam filtering)


More information about the Wellington-pm mailing list