Find a 3x3 matrix each of whose 9 entries is formed entirely by a non leading zeroes binary palindrome and:
The sum of each of the three rows = The sum of each of the three columns = the sum of exactly one of the two main diagonals = 477 (base ten)
*** Disregard rotations and reflections.
**** All the nine entries MUST be distinct.
Three solutions:
27 297 153
255 129 93
195 51 231
51 297 129
231 153 93
195 27 255
93 255 129
231 195 51
153 27 297
DefDbl A-Z
Dim crlf$, bpal(100), used(100), grid(3, 3), bpPtr
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
For i = 1 To 477
bi$ = base$(i, 2)
If isPal(bi) Then
bpPtr = bpPtr + 1
bpal(bpPtr) = i
Text1.Text = Text1.Text & i & crlf
End If
Next
Text1.Text = Text1.Text & bpPtr & crlf & crlf
addOn 1, 1
Text1.Text = Text1.Text & crlf & " done"
End Sub
Sub addOn(row, col)
DoEvents
For i = 1 To bpPtr
If used(i) = 0 Then
used(i) = 1
grid(row, col) = i
rowtot = 0
For j = 1 To col
rowtot = rowtot + bpal(grid(row, j))
Next
good = 1
If row = 3 Then
t = bpal(grid(1, col)) + bpal(grid(2, col)) + bpal(grid(3, col))
If t <> 477 Then
good = 0
End If
End If
If col = 3 Then
If rowtot <> 477 Then
good = 0
End If
End If
If good Then
If col < 3 Then
addOn row, col + 1
Else
If row < 3 Then
addOn row + 1, 1
Else
If bpal(grid(1, 1)) + bpal(grid(2, 2)) + bpal(grid(3, 3)) = 477 Or bpal(grid(1, 3)) + bpal(grid(2, 2)) + bpal(grid(3, 1)) = 477 Then
If grid(1, 1) < grid(1, 3) And grid(1, 1) < grid(3, 3) And grid(1, 1) < grid(3, 1) Then
If grid(1, 3) < grid(3, 1) Then
For r = 1 To 3
For c = 1 To 3
Text1.Text = Text1.Text & Str(bpal(grid(r, c)))
Next
Text1.Text = Text1.Text & crlf
Next
Text1.Text = Text1.Text & crlf
End If
End If
End If
End If
End If
End If
used(i) = 0
End If
Next
End Sub
Function base$(n, b)
v$ = ""
n2 = n
Do
d = n2 Mod b
n2 = n2 \ b
v$ = LTrim(Str(d)) + v$
Loop Until n2 = 0
base$ = v$
End Function
Function isPal(s$)
good = 1
For i = 1 To Len(s$) / 2
If Mid$(s$, i, 1) <> Mid$(s$, Len(s$) + 1 - i, 1) Then good = 0: Exit For
Next
isPal = good
End Function
|
Posted by Charlie
on 2016-01-21 15:48:43 |