Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

The optimum way to generate all possible combinations of 5 digits

orausernFeb 18 2014 — edited Feb 19 2014

Hi Experts,

I am on Oracle 11.2.0.2 on Linux. I have a task to generate all possible combinations of five digits (1 to 5) and each digit can be used only once. I wrote the code as below but I wonder if this is optimum - or if it can be improved on. i will be thankful for your commens about it:

DECLARE
BEGIN
   FOR i IN 1 .. 5
   LOOP
      FOR j IN 1 .. 5
      LOOP
         IF i = j
         THEN
            CONTINUE;
         ELSE
            NULL;
         END IF;

         FOR k IN 1 .. 5
         LOOP
            IF (j = k OR i = k)
            THEN
               CONTINUE;
            ELSE
               NULL;
            END IF;

            FOR l IN 1 .. 5
            LOOP
               IF (k = l OR i = l OR j = l)
               THEN
                  CONTINUE;
               ELSE
                  NULL;
               END IF;


               FOR m IN 1 .. 5
               LOOP
                  IF (l = m OR i = m OR j = m OR k = m)
                  THEN
                     CONTINUE;
                  ELSE
                     NULL;
                  END IF;

                  DBMS_OUTPUT.put_line (
                     i || ' ' || j || ' ' || k || ' ' || l || ' ' || m);
               END LOOP;
            END LOOP;
         END LOOP;
      END LOOP;
   END LOOP;
END;
/

Thnaks,

OrauserN

This post has been answered by Frank Kulash on Feb 19 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 19 2014
Added on Feb 18 2014
26 comments
3,976 views