Try to solve the 6 number 1 target android game using oracle (sql or pl/sql)

Gurus and mentors/friends,.

Today, had little space in my time to work, so I tried to reproduce an android game called "6 numbers between 1-target" in oracle.

All that I need help from you all is to make more effective/robust and garnish with a little bit of awesomeness (preferable in SQL ? I don't)

Please note that if you are busy, skip this question.

------------------------------------------------------------------------------------------------

Game goes like this:

-------------------------

computer gives you only 6 numbers...

for example: 50,9,5,8,6,7

And using mathematics as operators '+','-',' *', ' / ' we need to get a result comparable to 292.  (this number 292 is also given by computer..)

All that we need is the formula apt for 292 out of these 6 numbers (pouvez/do not use all these numbers) using these operators (you can use them without the restrictions of many times where they are used and also the decimal places are truncated.).

So solutions can be: 50 * 6 - 8 + 5/9/7, 50 * 6 - 8 or 50 * 6 - 8 + 5/7/9 etc...   (multiple answers are possible, all boiling down giving 292)

I just tried to write it this way, but its time consuming because levels rise. So I thought that I would be the dice in this forum for the best solution. (or maybe suggestions to improve my solution below)

------------------------------------


create table demo (str varchar2 (100), number of val);

-----------------------------------


DECLARE
l_str VARCHAR2 (100);
l_result NUMBER;
BEGIN
EXECUTE IMMEDIATE 'demo truncate table ';

WHILE (TRUE) LOOP - I know it's wrong, but it's the game after all and I needed iterations to go until I get to a solution.
BEGIN
FOR rec
IN (WITH operators AS
(Op SELECT COLUMN_VALUE
TABLE (sys.odcivarchar2list ('+',)
'-',
'*',
'/'))
ORDER OF DBMS_RANDOM. VALUE ()),
t AS
(SELECT SUBSTR (SYS_CONNECT_BY_PATH (letter, ','), 2))
Word
FROM (SELECT LEVEL LVL,
REGEXP_SUBSTR (str,
'[^,]+',
1,
LEVEL)
LETTER
FROM (SELECT '7,8,5,50,9,6' FROM DUAL str) t
CONNECT BY LEVEL < =.
REGEXP_COUNT (STR, ',') + 1).
WHERE = 6
CONNECT BY NOCYCLE lvl! = lvl PRIOR),
TT AS
(SELECT REGEXP_SUBSTR (Word,
'[^,]*',
1,
1)
col1,
(SELECT val
FROM (SELECT val op
Operators
ORDER OF DBMS_RANDOM. VALUE ())
WHERE ROWNUM = 1)
OP1,
REGEXP_SUBSTR (Word,
'[^,]*',
1,
3)
col2,
(SELECT val
FROM (SELECT val op
Operators
ORDER OF DBMS_RANDOM. VALUE ())
WHERE ROWNUM = 1)
OP2,
REGEXP_SUBSTR (Word,
'[^,]*',
1,
5)
col3,
(SELECT val
FROM (SELECT val op
Operators
ORDER OF DBMS_RANDOM. VALUE ())
WHERE ROWNUM = 1)
OP3,
REGEXP_SUBSTR (Word,
'[^,]*',
1,
7)
COL4,
(SELECT val
FROM (SELECT val op
Operators
ORDER OF DBMS_RANDOM. VALUE ())
WHERE ROWNUM = 1)
OP4,
REGEXP_SUBSTR (Word,
'[^,]*',
1,
9)
col5,
(SELECT val
FROM (SELECT val op
Operators
ORDER OF DBMS_RANDOM. VALUE ())
WHERE ROWNUM = 1)
OP5,
REGEXP_SUBSTR (Word,
'[^,]*',
1,
11)
col6
T)
SELECT col1
|| OP1
|| col2
|| OP2
|| COL3
|| OP3
|| COL4
|| OP4
|| col5
|| OP5
|| col6
formula
TT
ORDER OF DBMS_RANDOM. VALUE ()) LOOP
EXECUTE IMMEDIATE ' begin: result: = ' | Rec.Formula | '; end; »
With the HELP OF THE l_result;

l_str: = rec.formula;

INSERT INTO demo
VALUES (rec.formula, TRUNC (l_result));
-COMMIT;


END LOOP;
END;

IF (l_result = 292) THEN
EXIT;
END IF;
END LOOP;
END;
/

--------------------------------------------------------------------------------------------------

Thanks in advance!  (even for those who have opened this question )

See you soon,.

Manik.

EDIT: this solution is not all of the possible options. Go here for a shorter, faster, more complete solution.

Here's a solution that includes parentheses. It follows the rules of "game show" as odie_63 said: the result of an intermediate calculation must be a positive integer, and each integer entry can be used at most once.

There is some unnecessary parentheses, but they do not change the logic or the defined solution.

It works in about 70 seconds on my PC.

WITH input AS (
  select '50,9,8,7,6,5' nums, 292 targ from dual
), nums AS (
  select power(2, row_number() over(order by num desc) - 1) bits, num
  FROM (
    SELECT to_number(regexp_substr(nums,'[^,]+',1,ROWNUM)) num
    FROM input
    CONNECT BY to_number(regexp_substr(nums,'[^,]+',1,ROWNUM)) IS NOT NULL
  )
), ma_1 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, NULL, num, num, num||NULL, 0 FROM nums
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  n.op, n.num,
  decode(n.op,'+', o.cumul_num + n.num, o.cumul_num * n.num),
  o.str || n.op || n.num,
  1
  FROM ma_1 o
  JOIN (
    SELECT * FROM nums,
    (SELECT '*' op FROM dual UNION ALL SELECT '+' FROM dual)
  ) n
  ON nvl(o.op,n.op) = n.op
  AND n.num < o.num
), ds_2 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, 0, cumul_num, str, num_parens FROM ma_1
  where bits < 63 or cumul_num = (select targ from input)
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op, n.num,
  decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
  END,
  o.str || decode(o.op,'+','-','/') || n.num,
  1
  FROM ds_2 o
  JOIN nums n
  ON o.op IS NOT NULL
  and n.num > o.num
  AND bitand(o.BITS, n.BITS) = 0
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
    = trunc(decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num))
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num) > 0
), fmt_3 as (
  SELECT BITS, op, cumul_num num, cumul_num,
  case when num_parens = 1 and bits < 63 then '(' end
    || str ||
    case when num_parens = 1 and bits < 63 then ')' end str,
  num_parens
  FROM ds_2, input
  where bits < 63 or cumul_num = targ
), ma_4 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, num, cumul_num, str, num_parens FROM fmt_3
  UNION ALL
  SELECT
  (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op,
  n.num,
  decode(o.op,'*', o.cumul_num + n.num, o.cumul_num * n.num),
  o.str || decode(o.op,'*','+','*') || n.str,
  least(o.num_parens+1, 2)
  FROM ma_4 o
  JOIN fmt_3 n
  on o.num_parens > 0
  and o.op = nvl(n.op,o.op)
  AND n.num < o.num
  and bitand(o.bits, n.bits) = 0
), ds_5 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, 0, cumul_num, str, num_parens FROM ma_4
  where bits < 63 or cumul_num = (select targ from input)
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op, n.num,
  decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num)
  END,
  o.str || decode(o.op,'*','-','/') || n.str,
  least(o.num_parens+1, 2)
  FROM ds_5 o
  JOIN fmt_3 n
  on o.num_parens > 0
  and o.op = nvl(n.op,o.op)
  AND n.num > o.num
  and bitand(o.bits, n.bits) = 0
  AND decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num)
    = trunc(decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num))
  AND decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num) > 0
), fmt_6 as (
  SELECT BITS, op, cumul_num num, cumul_num,
  case when num_parens = 2 and bits < 63 then '(' end
    || str ||
    case when num_parens = 2 and bits < 63 then ')' end str,
  num_parens
  FROM ds_5, input
  where bits < 63 or cumul_num = targ
), ma_7 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, num, cumul_num, str, num_parens FROM fmt_6
  UNION ALL
  SELECT
  (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op,
  n.num,
  decode(o.op,'+', o.cumul_num + n.num, o.cumul_num * n.num),
  o.str || o.op || n.str,
  least(o.num_parens+1, 3)
  FROM ma_7 o
  JOIN fmt_6 n
  on o.num_parens > 1
  and n.num_parens != 1
  and o.op = nvl(n.op,o.op)
  AND n.num < o.num
  and bitand(o.bits, n.bits) = 0
), ds_8 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, 0, cumul_num, str, num_parens FROM ma_7
  where bits < 63 or cumul_num = (select targ from input)
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op, n.num,
  decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
  END,
  o.str || decode(o.op,'+','-','/') || n.str,
  least(o.num_parens+1, 3)
  FROM ds_8 o
  JOIN fmt_6 n
  on o.num_parens > 1
  and n.num_parens != 1
  and o.op = nvl(n.op,o.op)
  AND n.num > o.num
  and bitand(o.bits, n.bits) = 0
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
    = trunc(decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num))
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num) > 0
), fmt_9 as (
  SELECT BITS, op, cumul_num num, cumul_num,
  case when num_parens = 3 and bits < 63 then '(' end
    || str ||
    case when num_parens = 3 and bits < 63 then ')' end str,
  num_parens
  FROM ds_8, input
  where bits < 63 or cumul_num = targ
), ma_10 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, num, cumul_num, str, num_parens FROM fmt_9
  UNION ALL
  SELECT
  (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op,
  n.num,
  decode(o.op,'*', o.cumul_num + n.num, o.cumul_num * n.num),
  o.str || decode(o.op,'*','+','*') || n.str,
  least(o.num_parens+1, 4)
  FROM ma_10 o
  JOIN fmt_9 n
  on o.num_parens > 2
  and n.num_parens != 2
  and o.op = nvl(n.op,o.op)
  AND n.num < o.num
  and bitand(o.bits, n.bits) = 0
), ds_11 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, 0, cumul_num, str, num_parens FROM ma_10
  where bits < 63 or cumul_num = (select targ from input)
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op, n.num,
  decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num)
  END,
  o.str || decode(o.op,'*','-','/') || n.str,
  least(o.num_parens+1, 4)
  FROM ds_11 o
  JOIN fmt_9 n
  on o.num_parens > 2
  and n.num_parens != 2
  and o.op = nvl(n.op,o.op)
  AND n.num > o.num
  and bitand(o.bits, n.bits) = 0
  AND decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num)
    = trunc(decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num))
  AND decode(o.op, '*', o.cumul_num - n.num, o.cumul_num / n.num) > 0
), fmt_12 as (
  SELECT BITS, op, cumul_num num, cumul_num,
  case when num_parens = 4 and bits < 63 then '(' end
    || str ||
    case when num_parens = 4 and bits < 63 then ')' end str,
  num_parens
  FROM ds_11, input
  where bits < 63 or cumul_num = targ
), ma_13 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, num, cumul_num, str, num_parens FROM fmt_12
  UNION ALL
  SELECT
  (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op,
  n.num,
  decode(o.op,'+', o.cumul_num + n.num, o.cumul_num * n.num),
  o.str || o.op || n.str,
  least(o.num_parens+1, 5)
  FROM ma_13 o
  JOIN fmt_12 n
  on o.num_parens > 3
  and n.num_parens != 3
  and o.op = nvl(n.op,o.op)
  AND n.num < o.num
  and bitand(o.bits, n.bits) = 0
), ds_14 (BITS, op, num, cumul_num, str, num_parens) AS (
  SELECT BITS, op, 0, cumul_num, str, num_parens FROM ma_13
  where bits < 63 or cumul_num = (select targ from input)
  UNION ALL
  SELECT (o.BITS + n.BITS) - BitAND(o.BITS, n.BITS),
  o.op, n.num,
  decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
  END,
  o.str || decode(o.op,'+','-','/') || n.str,
  least(o.num_parens+1, 5)
  FROM ds_14 o
  JOIN fmt_12 n
  on o.num_parens > 3
  and n.num_parens != 3
  and o.op = nvl(n.op,o.op)
  AND n.num > o.num
  and bitand(o.bits, n.bits) = 0
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num)
    = trunc(decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num))
  AND decode(o.op, '+', o.cumul_num - n.num, o.cumul_num / n.num) > 0
)
select str, dbms_aw.eval_number(str) check_result
from (
  select str from ds_14, input
  where cumul_num = targ
)
order by str;
STR CHECK_RESULT
((((6*5) + 8) * 9)-50) 292
((((8*6) + 9) * 5) + 7) 292
((((8*6)-5) * 7)-9) 292
(((50*5) + 8) * 7/6)-9 292
(((50*9) + 6) * 5/8) + 7 292
(((50+8) * 5) + 9-7) 292
+ ((50*5) (7 * 6)) 292
((50*6) + 8-7-9) 292
((50*6)-8) 292
((50 + 6-5-8) * 7)-9 292
((50+7) * 5) + 9 + 6-8 292
((50 + 8-7) * 6)-5-9 292
((50 + 8-9) * 6) + 5-7 292
((50 + 9 + 6-8) * 5) + 7 292
((50 + 9 + 7-6) * 5)-8 292
(8 * 6 * 5) + 50 + 9 - 7 292

Tags: Database

Similar Questions

Maybe you are looking for

  • iTunes Store &gt; purchase question

    In iTunes, listed under iTunes Store > purchases, I see that the 250 most recent numbers that I bought. How can I access other (I bought up to 800 songs over the years). I bought a new iMac and to import my old iMac to my new songs purchased. The eas

  • Pavillion g7 nitebook: creation of recovery disks

    Just bought a refurbished laptop (new never been used) that comes with windows 8. I want to spend to 8.1 and then windows 10. My problem is that it's said I can only create a recovery disk once. If I create a recovery disc now until I have the upgrad

  • W520 keyboard

    Hello, I buy W520, by market research I founf this W520 is ideal for me. But in seeking, I have some confusion, I found (W520) this model have two different keyboard, one contains the Enter key rectangular staraight, and the other have the Enter key

  • Controlling the STDIN STDOUT of a legacy in LabVIEW application

    I have a legacy DOS base program that runs in a command line interface in Windows.  Normal use which is a type of operator in a command, look at the result, then type in another command based on the results.  I need launch this application, KEEP IT O

  • Printer goes offline on its own

    My first Officejet Pro 8500 (A909n) has suddenly gone 'offline '. When I go to tools, network, Wireless Setup Wizard"the printer detects my network and joins it.  I print something without any problem, but later, when I try to print something else, s