This equation: a+(13*b/c)+d+(12*e)-f+(g*h/i)=87 appears when trying to solve the maths puzzle for Vietnamese eight-year-olds that recently became viral all over
I was going to submit another answer but since my last answer was pretty off base I've just overwritten it. This still uses a Monte Carlo style random number approach but it gets a bit lumpy when you have to make sure you haven't already solved with that random number combination.
Sub MonteCarlo()
Dim startTime As Single
startTime = Timer
Dim trialSol As Double
Dim solCounter As Integer
solCounter = 0
Dim trialNums() As Integer
Dim solutions As Collection
Set solutions = New Collection
Dim existingSol As Boolean
existingSol = False
Do
trialNums = CreateRandomArray
trialSol = ToSolve(trialNums(1), trialNums(2), _
trialNums(3), trialNums(4), _
trialNums(5), trialNums(6), _
trialNums(7), trialNums(8), _
trialNums(9))
If trialSol = 87 Then
If Not ExistsIn(solutions, trialNums) Then
solutions.Add (trialNums)
End If
End If
Loop Until (solutions.Count = 128)
Dim solutionTime As Single
solutionTime = Round(Timer - startTime, 5)
Dim i As Integer
For i = 1 To solutions.Count
Debug.Print "Solution " & i & ":"; vbTab; _
solutions.Item(i)(1); vbTab; _
solutions.Item(i)(2); vbTab; _
solutions.Item(i)(3); vbTab; _
solutions.Item(i)(4); vbTab; _
solutions.Item(i)(5); vbTab; _
solutions.Item(i)(6); vbTab; _
solutions.Item(i)(7); vbTab; _
solutions.Item(i)(8); vbTab; _
solutions.Item(i)(9)
Next i
Debug.Print "Solution time: " & solutionTime & " ms"
End Sub
Function ExistsIn(col As Collection, arr() As Integer) As Boolean
Dim ei As Boolean
ei = False
Dim i As Integer
Dim temparr() As Integer
If col.Count > 0 Then
For i = 1 To col.Count
temparr = col.Item(i)
ei = AreEqual(temparr, arr)
Next i
End If
ExistsIn = ei
End Function
Function AreEqual(array1() As Integer, array2() As Integer) As Boolean
Dim eq As Boolean
eq = True
For i = LBound(array1) To UBound(array1)
If array1(i) <> array2(i) Then
eq = False
Exit For
End If
Next i
AreEqual = eq
End Function
Function ToSolve(a As Integer, b As Integer, _
c As Integer, d As Integer, _
e As Integer, f As Integer, _
g As Integer, h As Integer, _
i As Integer) As Double
ToSolve = a + (13 * b / c) + d + (12 * e) - f + (g * h / i)
End Function
Function CreateRandomArray() As Integer()
Dim numbers As New Collection
Dim i As Integer
For i = 1 To 9
numbers.Add i
Next i
Dim rndNums(9) As Integer
Dim rndInd As Integer
For i = 1 To 9
rndInt = CInt(((numbers.Count - 1) * Rnd) + 1)
rndNums(i) = numbers(rndInt)
numbers.Remove (rndInt)
Next i
CreateRandomArray = rndNums
End Function
My solution time for all combinations is around 3s - 3.5s.