A queen, a rook and a bishop are randomly placed on distinct squares of an ordinary chessboard.
Find the probability that:.
(i) The queen is under attack from either the bishop or the rook.
(ii) The bishop is neither under attack from the queen, nor under attack from the rook.
There is a bug in the program, and incorrect answer given. See later post.
DefDbl A-Z
Dim crlf$
Private Type fraction
num As Double
den As Double
End
Dim overProb As fraction, sitProb As fraction, unity As fraction
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
unity.num = 1: unity.den = 1
overProb.num = 0
overProb.den = 1
sitProb.num = 1
sitProb.den = 64# * 63 * 62
For qr = 1 To 8
For qc = 1 To 8
For br = 1 To 8
For bc = 1 To 8
If br <> qr Or bc <> bc Then
For rr = 1 To 8
For rc = 1 To 8
DoEvents
If (rr <> qr Or rc <> qc) And (rr <> br Or rc <> bc) Then
qrRowDiff = qr - rr: qrColDiff = qc - rc
qbRowDiff = qr - br: qbColDiff = qc - bc
If qrRowDiff = 0 Then hit = 1 Else hit = 0
If qbRowDiff = 0 Then If Sgn(qbColDiff) = Sgn(qrColDiff) And Abs(qbColDiff) < Abs(qrColDiff) Then hit = 0
If hit = 0 Then
If qrColDiff = 0 Then hit = 1 Else hit = 0
If qbColDiff = 0 Then If Sgn(qbRowDiff) = Sgn(qrRowDiff) And Abs(qbRowDiff) < Abs(qrRowDiff) Then hit = 0
End If
If hit = 0 Then
If Abs(qbRowDiff) = Abs(qbColDiff) Then hit = 1 Else hit = 0
If Abs(qrRowDiff) = Abs(qrColDiff) Then If Sgn(qrRowDiff) = Sgn(qbRowDiff) And Sgn(qrColDiff) = Sgn(qbColDiff) And Abs(qrColDiff) < Abs(qbColDiff) Then hit = 0
End If
If hit Then
overProb = add(overProb, sitProb): ctr = ctr + 1
If ctr Mod 1000 = 0 Then
Text1.Text = Text1.Text & qr & qc & " "
Text1.Text = Text1.Text & br & bc & " "
Text1.Text = Text1.Text & rr & rc & crlf
End If
End If
End If
Next
Next
End If
Next
Next
Next
Next
Text1.Text = Text1.Text & crlf & overProb.num & "/" & overProb.den & " done"
Text1.Text = Text1.Text & crlf & ctr & "/" & 64# * 63 * 62 & crlf
overProb.num = 0
overProb.den = 1
ctr = 0
For qr = 1 To 8
For qc = 1 To 8
For br = 1 To 8
For bc = 1 To 8
If br <> qr Or bc <> bc Then
For rr = 1 To 8
For rc = 1 To 8
DoEvents
If (rr <> qr Or rc <> qc) And (rr <> br Or rc <> bc) Then
brRowDiff = br - rr: brColDiff = bc - rc
qbRowDiff = qr - br: qbColDiff = qc - bc
If brRowDiff = 0 Then hit = 1 Else hit = 0
If hit = 0 Then
If brColDiff = 0 Then hit = 1 Else hit = 0
End If
If hit = 0 Then
If Abs(qbRowDiff) = Abs(qbColDiff) Then hit = 1 Else hit = 0
If Abs(qrRowDiff) = Abs(qrColDiff) Then If Sgn(qrRowDiff) = Sgn(qbRowDiff) And Sgn(qrColDiff) = Sgn(qbColDiff) And Abs(qrColDiff) < Abs(qbColDiff) Then hit = 0
End If
If qbRowDiff = 0 Then hit = 1
If qbColDiff = 0 Then hit = 1
If hit Then
overProb = add(overProb, sitProb): ctr = ctr + 1
If ctr Mod 1000 = 0 Then
Text1.Text = Text1.Text & qr & qc & " "
Text1.Text = Text1.Text & br & bc & " "
Text1.Text = Text1.Text & rr & rc & crlf
End If
End If
End If
Next
Next
End If
Next
Next
Next
Next
Text1.Text = Text1.Text & crlf & overProb.num & "/" & overProb.den & crlf
overProb.num = -overProb.num
overProb = add(unity, overProb)
Text1.Text = Text1.Text & crlf & overProb.num & "/" & overProb.den & " done"
Text1.Text = Text1.Text & crlf & 64# * 63 * 62 - ctr & "/" & 64# * 63 * 62 & crlf
End Sub
Function mform$(x, t$)
a$ = Format$(x, t$)
If Len(a$) < Len(t$) Then a$ = Space$(Len(t$) - Len(a$)) & a$
mform$ = a$
End Function
Private Function add(a As fraction, b As fraction) As fraction
Dim answ As fraction
If a.num = 0 Then a.den = 1
If b.num = 0 Then b.den = 1
d = lcm(a.den, b.den)
answ.num = a.num * d / a.den + b.num * d / b.den
If answ.num > 1E+15 Or answ.den > 1E+15 Then Text1.Text = Text1.Text & "OVERFLOW"
answ.den = d
If answ.num = 0 Then answ.den = 1
g = gcd(answ.num, answ.den)
answ.num = answ.num / g
answ.den = answ.den / g
add = answ
End Function
Private Function mult(a As fraction, b As fraction) As fraction
Dim answ As fraction
answ.num = a.num * b.num
answ.den = a.den * b.den
If answ.num > 1E+15 Or answ.den > 1E+15 Then Text1.Text = Text1.Text & "OVERFLOW": Exit Sub
g = gcd(answ.num, answ.den)
answ.num = answ.num / g
answ.den = answ.den / g
If answ.den < 0 Then answ.num = -answ.num: answ.den = -answ.den
mult = answ
End Function
Function gcd(a, b)
x = a: y = b
Do
q = Int(x / y)
z = x - q * y
x = y: y = z
Loop Until z = 0
gcd = x
End Function
Function lcm(a, b)
lcm = (a / gcd(a, b)) * b
If lcm > 1E+15 Then Text1.Text = Text1.Text & "OVERFLOW"
End Function
Produces these sets of hits
Part i:
Row, column pairs for every thousandth possible situation that has a hit
qn bp rook
11 86 71
12 78 37
13 68 78
14 62 15
15 54 11
16 52 62
17 52 13
18 47 58
21 43 63
22 33 31
23 14 43
23 84 13
24 68 44
25 57 45
26 48 24
27 38 51
28 33 68
31 15 81
32 12 62
32 78 36
33 62 37
34 45 71
35 24 45
35 88 33
36 72 16
37 58 27
38 54 48
41 36 31
42 31 38
43 18 33
43 82 33
44 62 48
45 34 55
46 15 42
46 77 43
47 65 66
48 61 41
51 42 84
52 37 12
53 26 45
53 86 78
54 68 56
55 42 75
56 23 46
56 83 82
57 74 53
58 67 43
61 52 23
62 44 71
63 34 73
64 16 68
64 81 34
65 56 28
66 44 18
67 23 78
68 13 36
68 86 64
71 82 33
72 63 51
73 55 37
74 47 22
75 35 71
76 21 51
76 87 52
77 68 71
78 61 73
81 54 71
82 55 14
83 52 73
84 48 65
85 41 82
86 33 81
87 25 83
88 18 48
223/744 (the answer for part i)
74928/249984 (unreduced fraction -- raw counts -- for part i)
part ii:
(these are the hits, and therefore the opposite of what is sought)
again: only every thousandth hit situation is shown.
11 66 74
12 43 41
13 23 26
13 68 27
14 44 48
15 26 23
15 73 77
16 48 88
17 31 37
17 77 65
18 58 57
21 34 35
21 84 88
22 56 57
23 33 25
23 78 27
24 51 41
25 17 67
25 71 73
26 46 84
27 17 14
27 68 88
28 48 37
31 13 76
31 73 77
32 45 46
33 13 66
33 64 61
34 26 21
34 78 67
35 53 45
36 16 84
36 66 53
37 45 46
37 87 75
38 67 68
41 32 64
42 12 74
42 64 68
43 32 54
43 83 65
44 54 16
45 15 31
45 64 61
46 26 74
46 76 42
47 54 84
48 18 47
48 77 47
51 42 54
52 22 64
52 74 57
53 42 43
54 14 37
54 63 86
55 25 18
55 73 86
56 36 64
56 86 32
57 64 61
58 28 37
58 86 83
61 52 42
62 32 53
62 84 46
63 52 32
64 24 88
64 74 56
65 38 78
65 87 64
66 55 48
67 25 28
67 78 56
68 47 67
71 24 84
71 82 12
72 52 22
73 27 67
73 81 82
74 47 65
75 23 63
75 65 82
76 43 65
77 14 17
77 64 84
78 36 46
81 13 63
81 61 71
82 42 63
83 22 21
83 67 47
84 51 48
85 25 71
85 74 36
86 53 65
87 27 85
87 77 34
88 55 18
110/279 (prob of hit; therefore the negation of what's wanted in part ii)
169/279 (the answer for part ii)
151424/249984 (raw numbers -- i.e., case count -- unreduced fraction -- of the answer for part ii)
Edited on August 30, 2016, 9:31 pm
|
Posted by Charlie
on 2016-08-29 16:31:03 |