Is the answer 4 and 13?
I have heard this riddle a long time ago and tried to program is but didn't finnish it. Today I found the riddle back on this forum and programmed it again. My program comes with 4 and 13 (as the only solution), is this correct ?
-Ronald
This is the program:
Sub main()
For b = 2 To 98
For a = 2 To 98
If a + b ' Debug.Print a; b,
If [u]Not[/u] P_knows(a * b) Then
' Debug.Print "P knows not",
If S_knows_that(a + b) Then
' Debug.Print "S knows that",
If P_knows_now(a * b) Then
' Debug.Print "P knows now",
If S_knows_now(a + b) Then
' Debug.Print "S knows now",
MsgBox Format(a) + ", " + Format(b)
End If: End If: End If: End If: End If
' Debug.Print
' DoEvents
Next a, b
End Sub
Function P_knows(Product As Integer) 'Does P know the numbers?
t = 0
For I = 2 To Sqr(Product) 'Sqr to exclude the same numbers (e.g. 8 = 2 * 4 and 4 * 2)
If Product Mod I = 0 Then t = t + 1 'P doesn't know if you can divide it by more then one number
If t > 1 Then Exit For 'no need to check futher
Next
P_knows = t = 1
End Function
Function S_knows_that(Sum) 'Does S know that P can't know the numbers?
For I = 2 To Sum 2 ' 2 to exclude the same numbers (e.g. 17 = 2 + 15 and 15 + 2)
If P_knows(i * (Sum - i)) Then 'for all the possible sums P should not be able to know the combination
S_knows_that = False
Exit Function
End If
Next i
S_knows_that = True
End Function
Function P_knows_now(Product) 'Does P know the two numbers after S knows that P can't know them?
t = 0
For I = 2 To Sqr(Product)
If Product Mod I = 0 Then 'P can only say that if there is just one combination
If I + (Product i) If t > 1 Then Exit For 'no need to check futher
End If
Next i
P_knows_now = t = 1
End Function
Function S_knows_now(Sum) 'Does S know the two numbers too after P knows them?
t = 0
For I = 2 To Sum 2 'S can only say that if there is just one combination
If P_knows_now(i * (Sum - i)) Then t = t + 1
If t > 1 Then Exit For 'no need to check futher
Next i
S_knows_now = t = 1
End Function