Reputation: 173
I want to write this function from here: https://math.stackexchange.com/questions/721494/what-is-the-value-of-this-game
I have written the following, but it doesn't work.
Function value(b As Integer, r As Integer)
If r = 0 Then
value = 0
End If
If b = 0 And r > 0 Then
value = r
End If
If (b < 0 Or r <= 0) Then
value = 0
End If
value(b, r) = (b / (b + r)) * (-1 + value(b - 1, r)) + (r / (b + r)) * (1 + value(b, r-1 ))
End Function
Can someone explain why it doesn't work, I am very new to VBA and programming.
Upvotes: 1
Views: 135
Reputation: 35843
This should work:
Function gameValue(b As Integer, r As Integer)
If b < 0 Or r <= 0 Then
gameValue = 0
Exit Function
End If
If b = 0 And r > 0 Then
gameValue = r
Exit Function
End If
gameValue = (b / (b + r)) * (-1 + gameValue(b - 1, r)) + (r / (b + r)) * (1 + gameValue(b, r - 1))
End Function
Note that I've changed function name from value
to gameValue
. The reason is because there is built-in excel worksheet function with name VALUE
Upvotes: 1