How to solve the Length 9 puzzle in SQL
October 23rd, 2007 By Frank Zhou
The following is an interesting puzzle posted on Usenet rec.puzzles archive:
Is it possible to make a number and its square, using the digits from 1 through 9 exactly once?
———————-SQL solution————————
SELECT num, square
FROM
(SELECT LEVEL as num, power(LEVEL, 2) square FROM dual
WHERE translate( '123456789', 'X'||power(LEVEL,2)||LEVEL,'X') IS NULL
CONNECT BY to_number(power(LEVEL, 2)||LEVEL) <= 987654321
);
NUM SQUARE
---------- ----------
567 321489
854 729316
Elapsed: 00:00:00.31
