|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 2:32 AM
Points: 22,
Visits: 1,100
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
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!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
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!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, June 17, 2013 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
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!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 4:16 PM
Points: 1,091,
Visits: 2,205
|
|
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(<>) 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. Please don't trust me, test the solutions I give you before using them. Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 22, 2013 2:32 AM
Points: 22,
Visits: 1,100
|
|
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 .... 
I don't think it can get any better.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
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!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, June 17, 2013 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
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(<>) 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
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, June 17, 2013 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
Shaun-884394 (9/12/2012)
... GSquared i love your signature ROFL,LOL,ETC ....  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
|
|
|
|