All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 Random Chess Pieces Poser (Posted on 2016-08-29)
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.

 No Solution Yet Submitted by K Sengupta No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
 computer aided solution | Comment 1 of 6
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

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

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
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    7112    78    3713    68    7814    62    1515    54    1116    52    6217    52    1318    47    5821    43    6322    33    3123    14    4323    84    1324    68    4425    57    4526    48    2427    38    5128    33    6831    15    8132    12    6232    78    3633    62    3734    45    7135    24    4535    88    3336    72    1637    58    2738    54    4841    36    3142    31    3843    18    3343    82    3344    62    4845    34    5546    15    4246    77    4347    65    6648    61    4151    42    8452    37    1253    26    4553    86    7854    68    5655    42    7556    23    4656    83    8257    74    5358    67    4361    52    2362    44    7163    34    7364    16    6864    81    3465    56    2866    44    1867    23    7868    13    3668    86    6471    82    3372    63    5173    55    3774    47    2275    35    7176    21    5176    87    5277    68    7178    61    7381    54    7182    55    1483    52    7384    48    6585    41    8286    33    8187    25    8388    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    7412    43    4113    23    2613    68    2714    44    4815    26    2315    73    7716    48    8817    31    3717    77    6518    58    5721    34    3521    84    8822    56    5723    33    2523    78    2724    51    4125    17    6725    71    7326    46    8427    17    1427    68    8828    48    3731    13    7631    73    7732    45    4633    13    6633    64    6134    26    2134    78    6735    53    4536    16    8436    66    5337    45    4637    87    7538    67    6841    32    6442    12    7442    64    6843    32    5443    83    6544    54    1645    15    3145    64    6146    26    7446    76    4247    54    8448    18    4748    77    4751    42    5452    22    6452    74    5753    42    4354    14    3754    63    8655    25    1855    73    8656    36    6456    86    3257    64    6158    28    3758    86    8361    52    4262    32    5362    84    4663    52    3264    24    8864    74    5665    38    7865    87    6466    55    4867    25    2867    78    5668    47    6771    24    8471    82    1272    52    2273    27    6773    81    8274    47    6575    23    6375    65    8276    43    6577    14    1777    64    8478    36    4681    13    6381    61    7182    42    6383    22    2183    67    4784    51    4885    25    7185    74    3686    53    6587    27    8587    77    3488    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

 Search: Search body:
Forums (0)