OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

How to solve the Feynman’s Puzzle in SQL

June 9th, 2010 By Frank Zhou

The following is an interesting puzzle posted on the programming praxis website:

Feynman’s Puzzle

This Puzzles was proposed by Richard Feyman ( An American physicist)

‘A’ can be any digit; each ‘.’ can be any digit from 0 through 9 except ‘A’. There is only one solution.


         ..A.
    ---------
.A. ) ....A..
      ..AA
      ----
       ...A
        ..A
       -----
        ....
        .A..
        -----
         ....
         ....
         ----
            0



COLUMN str FORMAT  A38
COLUMN d_e_A_f FORMAT A8
COLUMN b_A_c  FORMAT  A8

-----------------------------------------------------------SQL Solution----------------------------------------------------------------------------




WITH DATA1 AS
(SELECT LEVEL n FROM DUAL CONNECT BY LEVEL < 10 ),
DATA2 AS
(SELECT LEVEL-1 n FROM DUAL CONNECT BY LEVEL  < 11 )
SELECT d_e_A_f||chr(10)||'     ------------'||chr(10)||b_A_c||'  )'||Div as str, b_A_c, d_e_A_f
FROM
(SELECT b.n|| A.n|| c.n as b_A_c ,  d.n||e.n||A.n||f.n  as d_e_A_f , to_number(b.n|| A.n|| c.n )* to_number(d.n||e.n||A.n||f.n) as Div
 FROM data2 A,  data1 b, data2 c,  data1 d,  data2 e, data2 f 
 WHERE b.n != A.n
 AND   c.n != A.n
 AND   d.n != A.n
 AND   e.n != A.n
 AND   f.n != A.n
 AND length ((b.n * 100 + A.n *10 + c.n) *  (d.n *1000 + e.n *100 + A.n *10 + f.n)) =7
 AND to_number(substr((b.n * 100 + A.n *10 + c.n) *  (d.n *1000 + e.n *100 + A.n *10 + f.n), 5, 1 ) ) = A.n
 AND to_number(substr((b.n * 100 + A.n *10 + c.n) * d.n, 3, 2 ))  = to_number(A.n||A.n)
 AND to_number(substr((b.n * 100 + A.n *10 + c.n)*e.n, 3, 1 )) = A.n
 AND to_number(substr((b.n * 100 + A.n *10 + c.n) * A.n , 2, 1 ) ) = A.n
 AND e.n < d.n
);




STR                                    B_A_C      D_E_A_F
---------------------------           --------    --------
         7289                           484        7289
     ------------
484  )3527876


SQL>

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question