Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Puzzle #... 9?: Make 28 out of 4 values 3

Status
Not open for further replies.
With or without braces () ?

------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why

[banghead]
 
I assume we should ONLY use each number ONCE. Ex. The following is NOT an acceptable answer.

Select 5 + 5 + 5 + 4 + 4 + 3 + 2

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Ok. I got an answer. Kinda tricky though.

Code:
[white]Select 5*3/2*4[/white]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Well, my code didn't account for braces. It was VERY brute force, and braces would only have made it worse.

Here's the code I came up with. Prepare to laugh!

Code:
Declare @Temp 
Table(RowId Integer Identity(1,1), Attempt VarChar(50))

Insert Into @Temp(Attempt)
Select	Convert(VarChar(1), N1) + Op1 + Convert(VarChar(1), N2) + Op2 + Convert(VarChar(1), N3) + Op3 + Convert(VarChar(1), N4)
From	(
		Select 	A.Number As N1, 
				B.Number As N2, 
				C.Number As N3, 
				D.Number As N4
		From	(Select 2 As Number Union All Select 3 Union All Select 4 Union All Select 5) A
				Cross Join 
				(Select 2 As Number Union All Select 3 Union All Select 4 Union All Select 5) b
				Cross Join 
				(Select 2 As Number Union All Select 3 Union All Select 4 Union All Select 5) c
				Cross Join 
				(Select 2 As Number Union All Select 3 Union All Select 4 Union All Select 5) d
		Where	A.Number <> B.Number
				And A.Number <> C.Number
				And A.Number <> D.Number
				And B.Number <> C.Number
				And B.Number <> D.Number
				And C.Number <> D.Number
		) A
		Cross Join 
		(
		Select 	A.Operation As Op1, 
				B.Operation As Op2, 
				C.Operation As Op3
		From	(Select '+' As Operation Union All Select '-' Union All Select '*' Union All Select '/') A
				Cross Join 
				(Select '+' As Operation Union All Select '-' Union All Select '*' Union All Select '/') b
				Cross Join 
				(Select '+' As Operation Union All Select '-' Union All Select '*' Union All Select '/') c
		) B

Declare @i Integer
Declare @Attempt VarChar(10)
Declare @Max Integer
Declare @SQL VarChar(1000)
Declare @Answer VarChar(100)

Select	@i = 1,
		@Max = Max(RowId)
From	@Temp

Create Table #Temp(Answer Integer, Attempt VarChar(100))

While @i <= @Max
	Begin
		Select 	@Attempt = Attempt
		From	@Temp
		Where	RowId = @i

		Set @SQL = 'Select ' + @Attempt + ' As Answer, ''' + @Attempt + ''' As Attempt'

		Insert Into #Temp
		Exec (@SQL) 

		Set @i = @i + 1
	End

Select * From #Temp Where Answer = 28 Order By Answer

Drop Table #Temp

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
-- how about with 4 numbers 9 create 100

4 numbers... literally [smile]

select 99+9/9

------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why

[banghead]
 
Hi SQLDenis,

your solution:

select 4*(5+6/3)

is indeed interesting considering the allowed values are 2,3,4 and 5

Regards,

Atomic Wedgie

 
OK, stealth error [smile]. He probably meant

select 4*(5*2-3)

------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why

[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top