2 ? 3 ? 4 ? 5 ? 6 = 10
To make the above equation valid replace each of the four ? marks by any operator of the set (+, -, /, *) and unlimited quantity of brackets.
Rem1: No other operators/signs allowed.
Rem2: Analytical solution preferred.
A modification of the code for the puzzle "80" (thus explaining the extraneous code):
DefDbl A-Z
Dim crlf$, puzz(13) As String, stack(6), concatable(6), stackPtr
Dim expstr As String, pz As String
Private Sub Form_Load()
Form1.Visible = True
puzz(1) = "23456"
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
For pNo = 1 To 1
pz$ = puzz(pNo)
Text1.Text = Text1.Text & pz & crlf
h$ = pz
Do
stackPtr = 1
stack(1) = Val(Left(pz, 1)): concatable(1) = 0
expstr = Left(pz, 1)
addOn (2)
' permute pz
Loop Until pz = h
Next
Text1.Text = Text1.Text & crlf & " done"
End Sub
Sub addOn(wh)
ReDim sstack(6)
ReDim sconcatable(6)
DoEvents
For typ = 1 To 5
sstackPtr = stackPtr
sexpstr$ = expstr
For i = 1 To stackPtr
sstack(i) = stack(i)
sconcatable(i) = concatable(i)
Next
good = 1
Select Case typ
' Case 0
' If concatable(stackPtr) Then
' stack(stackPtr) = 10 * stack(stackPtr) + Val(Mid(pz, wh, 1))
' If Right(expstr, 1) = "," Then expstr = Left(expstr, Len(expstr) - 1)
' expstr = expstr + Mid(pz, wh, 1)
' Else
' good = 0
' End If
Case 1
stack(stackPtr) = stack(stackPtr) + Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1) + "+"
concatable(stackPtr) = 0
Case 2
stack(stackPtr) = stack(stackPtr) - Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1) + "-"
concatable(stackPtr) = 0
Case 3
stack(stackPtr) = stack(stackPtr) * Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1) + "*"
concatable(stackPtr) = 0
Case 4
If Val(Mid(pz, wh, 1)) > 0 Then
stack(stackPtr) = stack(stackPtr) / Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1) + "/"
concatable(stackPtr) = 0
Else
good = 0
End If
Case 5 ' comma
stackPtr = stackPtr + 1
stack(stackPtr) = Val(Mid(pz, wh, 1))
concatable(stackPtr) = 1
expstr = expstr + "," + Mid(pz, wh, 1)
Case 6
If stack(stackPtr) < 1000 Then
stack(stackPtr) = stack(stackPtr) ^ Val(Mid(pz, wh, 1))
expstr = expstr + Mid(pz, wh, 1) + "^"
concatable(stackPtr) = 0
Else
good = 0
End If
End Select
If good Then
If stackPtr = 1 Then
If stack(stackPtr) = 10 And Len(expstr) = 9 Then
Text1.Text = Text1.Text & " " & expstr & crlf
End If
End If
If wh < 6 Then addOn wh + 1
End If
expstr = sexpstr
stackPtr = sstackPtr
For i = 1 To stackPtr
stack(i) = sstack(i)
concatable(i) = sconcatable(i)
Next
Next
End Sub
finds the RPN:
23+4+5-6+
23+4*5/6+
23/4-5+6*
which translates to:
2+3+4-5+6
(2+3)*4/5+6
(2/3-4+5)*6
|
Posted by Charlie
on 2016-05-09 15:45:58 |