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 Multiples of single digits Puzzle in SQL

September 19th, 2007 By Frank Zhou

The following is an interesting puzzle posted by mathpuzzle:

Multiples of single digits: The number 24 is the product of single digits, in two different ways: 4×6 or 3×8. 3256 has consecutive pairs 32, 25, 56, and each is the product of single digits (4×8, 5×5, and 7×8).

Arrange the digits of 1-9 so that every consecutive pair is the product of single digits.

—————-SQL solution—————————


SELECT to_number(replace(sys_connect_by_path( n,','),',')) num
FROM
(SELECT LEVEL n FROM dual CONNECT BY LEVEL <= 9)
 WHERE LEVEL = 9
 CONNECT BY NOCYCLE n != PRIOR n
 AND
 (    (mod(to_number( PRIOR N||N),9) = 0 AND to_number( PRIOR N||N)<= 9*9)
  OR (mod(to_number( PRIOR N||N),8) = 0 AND to_number( PRIOR N||N)<= 8*9)
  OR (mod(to_number( PRIOR N||N),7) = 0 AND to_number( PRIOR N||N)<= 7*9)
  OR (mod(to_number( PRIOR N||N),6) = 0 AND to_number( PRIOR N||N)<= 6*9)
  OR (mod(to_number( PRIOR N||N),5) = 0 AND to_number( PRIOR N||N)<= 5*9)
  OR (mod(to_number( PRIOR N||N),4) = 0 AND to_number( PRIOR N||N)<= 4*9)
  OR (mod(to_number( PRIOR N||N),3) = 0 AND to_number( PRIOR N||N)<= 3*9)
  OR (mod(to_number( PRIOR N||N),2) = 0 AND to_number( PRIOR N||N)<= 2*9)
 );

NUM
----------
 728163549

Elapsed: 00:00:00.06

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question