SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Puzzle : Generating two unique numbers from an array of numbers (repetation not allowed)


Puzzle : Generating two unique numbers from an array of numbers (repetation not allowed)

Author
Message
Shaun-884394
Shaun-884394
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 1121
Hi All,

(This is just for fun, so you may choose to ignore this topic.)

Today I read a puzzle somewhere which is mentioned below.
I thought why not solve it by some SQL query.

Puzzle : Use the numbers 1,2,3,4,5,6,7,8,9 only once to fill in the blanks and complete the equation.
(4 _ _ _ 6) - (_ _ _ _) = 33333

As you see for the equation we have to generate 2 numbers, first a 3 digit number and second a 4 digit number and none of the digits in both these numbers can be repeated.

So here is the query i tried and it worked fine.
But i was wondering whether the negation joins i have used is the only way to achieve this?
Is there any smarter/efficient way to crack this puzzle?


with a(num) as
(select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)
,b(num1,num2) as
(
select
convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6')
,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + convert(varchar,g.num))
from
a
join a as b on a.num <> b.num
join a as c on a.num <> b.num and a.num <> c.num
and b.num <> c.num
join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num
and b.num <> c.num and b.num <> d.num
and c.num <> d.num
join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num
and c.num <> d.num and c.num <> e.num
and d.num <> e.num
join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num
and c.num <> d.num and c.num <> e.num and c.num <> f.num
and d.num <> e.num and d.num <> f.num
and e.num <> f.num
join a as g on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num and a.num <> g.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num and b.num <> g.num
and c.num <> d.num and c.num <> e.num and c.num <> f.num and c.num <> g.num
and d.num <> e.num and d.num <> f.num and d.num <> g.num
and e.num <> f.num and e.num <> g.num
and f.num <> g.num
)
select * from b
where num1-num2 = 33333
order by 1,2


Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12118 Visits: 5478
You don't really need last join as the last digit of the second number can only be '3'


with a(num) as
(select 1 union select 2 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)
,b(num1,num2) as
(
select
convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6')
,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3')
from
a
join a as b on a.num <> b.num
join a as c on a.num <> b.num and a.num <> c.num
and b.num <> c.num
join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num
and b.num <> c.num and b.num <> d.num
and c.num <> d.num
join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num
and c.num <> d.num and c.num <> e.num
and d.num <> e.num
join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num
and c.num <> d.num and c.num <> e.num and c.num <> f.num
and d.num <> e.num and d.num <> f.num
and e.num <> f.num
)
select distinct *, num1-num2 from b
where num1-num2 = 33333
order by 1,2



Also, I thought that you shouldn't be able to use 4 and 6 as they were already used by the first number. In this case you will get only single possible result:


with a(num) as
(select 1 union select 2 union select 5 union select 7 union select 8 union select 9)
,b(num1,num2) as
(
select
convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6')
,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3')
from
a
join a as b on a.num <> b.num
join a as c on a.num <> b.num and a.num <> c.num
and b.num <> c.num
join a as d on a.num <> b.num and a.num <> c.num and a.num <> d.num
and b.num <> c.num and b.num <> d.num
and c.num <> d.num
join a as e on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num
and c.num <> d.num and c.num <> e.num
and d.num <> e.num
join a as f on a.num <> b.num and a.num <> c.num and a.num <> d.num and a.num <> e.num and a.num <> f.num
and b.num <> c.num and b.num <> d.num and b.num <> e.num and b.num <> f.num
and c.num <> d.num and c.num <> e.num and c.num <> f.num
and d.num <> e.num and d.num <> f.num
and e.num <> f.num
)
select distinct *, num1-num2 from b
where num1-num2 = 33333
order by 1,2



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12118 Visits: 5478
Also, you don't need to check if the same digits from different joins are not equal (eg. if checked a.num<>b.num in one "join" you don't need to check it again in other "joins"). Therefore all JOINs can be rewritten using "incremented" NOT IN:



with a(num) as
(select 1 union select 2 union select 5 union select 7 union select 8 union select 9)
,b(num1,num2) as
(
select
convert(bigint, '4' + convert(varchar,a.num) + convert(varchar,b.num) + convert(varchar,c.num) + '6')
,convert(bigint, convert(varchar,d.num) + convert(varchar,e.num) + convert(varchar,f.num) + '3')
from
a
join a as b on a.num <> b.num
join a as c on c.num NOT IN (a.num, b.num)
join a as d on d.num NOT IN (a.num, b.num, c.num)
join a as e on e.num NOT IN (a.num, b.num, c.num, d.num)
join a as f on f.num NOT IN (a.num, b.num, c.num, d.num, e.num)
)
select distinct *, num1-num2 from b
where num1-num2 = 33333
order by 1,2




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56371 Visits: 9730
Here's what I came up with. Does the same thing the same way, just another way to write it:

WITH    Numbers
AS (SELECT *
FROM ( VALUES ( '1'), ( '2'), ( '3'), ( '4'), ( '5'), ( '6'), ( '7'), ( '8'), ( '9') ) AS Nums (Number))
SELECT N1.Number + N2.Number + N3.Number + N4.Number + N5.Number, N6.Number + N7.Number + N8.Number + N9.Number
FROM Numbers AS N1
CROSS APPLY (SELECT Number
FROM Numbers AS N2
WHERE N2.Number != N1.Number) AS N2
CROSS APPLY (SELECT Number
FROM Numbers AS N3
WHERE N3.Number NOT IN (N1.Number, N2.Number)) AS N3
CROSS APPLY (SELECT Number
FROM Numbers AS N4
WHERE N4.Number NOT IN (N1.Number, N2.Number, N3.Number)) AS N4
CROSS APPLY (SELECT Number
FROM Numbers AS N5
WHERE N5.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number)) AS N5
CROSS APPLY (SELECT Number
FROM Numbers AS N6
WHERE N6.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number)) AS N6
CROSS APPLY (SELECT Number
FROM Numbers AS N7
WHERE N7.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number)) AS N7
CROSS APPLY (SELECT Number
FROM Numbers AS N8
WHERE N8.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number,
N7.Number)) AS N8
CROSS APPLY (SELECT Number
FROM Numbers AS N9
WHERE N9.Number NOT IN (N1.Number, N2.Number, N3.Number, N4.Number, N5.Number, N6.Number,
N7.Number, N8.Number)) AS N9
WHERE N1.Number = '4'
AND N5.Number = '6'
AND CAST(N1.Number + N2.Number + N3.Number + N4.Number + N5.Number AS INT)
- CAST(N6.Number + N7.Number + N8.Number + N9.Number AS INT) = 33333



- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12118 Visits: 5478
As said before, you don't need to guess the last digit of the second number as it can be only 3!
Saves one cross-apply...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41086 Visits: 19815
Eugene Elutin (9/12/2012)
You don't really need last join as the last digit of the second number can only be '3'


Wouldn't that be cheating?
Because that way I can get the best performance

SELECT 41286 num1,
7953 num2



I had my solution that was very similar to yours (again) but using the different(<>Wink operators instead of the NOT IN.

However, a better performance would be using the best datatype for the digits (char) since the beginning avoiding additional conversions.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Shaun-884394
Shaun-884394
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 1121
Thank you Eugene and GSquared.

Eugene, the NOT IN looks like a much better/easier/readable join.

GSquared i love your signature ROFL,LOL,ETC .... :-D

I don't think it can get any better.
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12118 Visits: 5478
Luis Cazares (9/12/2012)
Eugene Elutin (9/12/2012)
You don't really need last join as the last digit of the second number can only be '3'


Wouldn't that be cheating?
....


Cheating? No, it's a common sense. There is no other number you can deduct from 6 to get 3. There is no need to guess that.
Regarding performance... I don't think it is relevant to this kind of task at all.
I guess finding the more "elegant" looking solution is what OP is after.
Saying that, removing number of casting will make it look more elegant:


;with a (num)
as
( select * from (values ('1'),('2'),('5'),('7'),('8'),('9')) a(num) )
,b
as
(
select '4' + a.num + b.num + c.num + '6' as num1
,d.num + e.num + f.num + '3' as num2
from a
join a as b on a.num <> b.num
join a as c on c.num NOT IN (a.num, b.num)
join a as d on d.num NOT IN (a.num, b.num, c.num)
join a as e on e.num NOT IN (a.num, b.num, c.num, d.num)
join a as f on f.num NOT IN (a.num, b.num, c.num, d.num, e.num)
)
select * from b
where cast(num1 as int) - cast(num2 as int)= 33333




_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56371 Visits: 9730
Luis Cazares (9/12/2012)
Eugene Elutin (9/12/2012)
You don't really need last join as the last digit of the second number can only be '3'


Wouldn't that be cheating?
Because that way I can get the best performance

SELECT 41286 num1,
7953 num2



I had my solution that was very similar to yours (again) but using the different(<>Wink operators instead of the NOT IN.

However, a better performance would be using the best datatype for the digits (char) since the beginning avoiding additional conversions.

(emphasis added)

That's why I did mine that way. Makes the concatenation, etc., more efficient, at the cost of the final Where clause being slightly more costly.

I also kept the rules for what fixed numbers go in what columns in the Where clause, instead of in the various From objects, because that way it can be modified for related queries more easily. If, for example, the puzzle were modified to "second number must start with '79'" instead of "first number must start with '4' and end with '6'", then a simple modification of the Where clause would make my version work.

For related puzzles, you could assign a parameter to each column. If not null, then fixed value for that column. Final value could also be a parameter.

And, yeah, I really do think extensibility like that with every piece of code I write.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56371 Visits: 9730
Shaun-884394 (9/12/2012)
...

GSquared i love your signature ROFL,LOL,ETC .... :-D

I don't think it can get any better.


Thanks! :-)

The really funny part is, there was a noticable increase in people assuming my posts were credible, when I added all that to my sig.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search