Fill a 3x3 square with the numbers from 1 to 9, so the four sides and the two diagonals share the same sum, which is to be as large as possible.
The largest value is 18, as already posted
6 4 8
5 3 1
7 2 9
No extinct birds used.
Visual Basic program (it ended in 57 seconds):
Imports System
Imports System.Runtime.InteropServices
Imports System.Math
Module Module1
Sub Main()
Randomize()
Dim strStartTime As String
Dim strEndTime As String
Dim strEnd As String
Dim intMaxVal As Integer = 0
Dim intArray(9)
Dim dblCount1 As Double = 0
Dim dblCount2 As Double = 0
strStartTime = TimeOfDay
Console.WriteLine("Start of execution: " & strStartTime)
For Index88 As Integer = 0 To 8
intArray(Index88) = 0
Next
For Index1 As Integer = 1 To 9
For Index2 As Integer = 1 To 9
If Index2 <> Index1 Then
SubIndex2(intMaxVal, intArray, Index1, Index2, _
dblCount1, dblCount2)
End If
Next
Next
Console.WriteLine("The maximum array found:")
For Index99 As Integer = 0 To 8
Console.WriteLine(Str(intArray(Index99)))
Next
Console.WriteLine("Start of execution: " & strStartTime)
strEndTime = TimeOfDay
Console.WriteLine("End of execution: " & strEndTime)
strEnd = "?"
While strEnd <> "X"
Console.WriteLine("Please enter X to exit program...")
strEnd = UCase(Console.ReadLine())
End While
End Sub
Sub SubIndex2(ByRef intMaxVal, ByRef intArray, _
ByRef Index1, ByRef Index2, ByRef dblCount1, ByRef dblCount2)
For Index3 As Integer = 1 To 9
If Index3 <> Index1 And _
Index3 <> Index2 Then
SubIndex3(intMaxVal, intArray, _
Index1, Index2, Index3, _
dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex3(ByRef intMaxVal, ByRef intArray, _
ByRef Index1, ByRef Index2, ByRef Index3, _
ByRef dblCount1, ByRef dblCount2)
For Index4 As Integer = 1 To 9
If Index4 <> Index1 And _
Index4 <> Index2 And _
Index4 <> Index3 Then
SubIndex4(intMaxVal, intArray, _
Index1, Index2, Index3, Index4, _
dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex4(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, _
ByRef dblCount1, ByRef dblCount2)
For Index5 As Integer = 1 To 9
If Index5 <> Index1 And _
Index5 <> Index2 And _
Index5 <> Index3 And _
Index5 <> Index4 Then
SubIndex5(intMaxVal, intArray, _
Index1, Index2, Index3, Index4, Index5, _
dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex5(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, ByRef Index5, _
ByRef dblCount1, ByRef dblCount2)
For Index6 As Integer = 1 To 9
If Index6 <> Index1 And _
Index6 <> Index2 And _
Index6 <> Index3 And _
Index6 <> Index4 And _
Index6 <> Index5 Then
SubIndex6(intMaxVal, intArray, _
Index1, Index2, Index3, Index4, Index5, Index6, _
dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex6(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, ByRef Index5, _
ByRef Index6, ByRef dblCount1, ByRef dblCount2)
For Index7 As Integer = 1 To 9
If Index7 <> Index1 And _
Index7 <> Index2 And _
Index7 <> Index3 And _
Index7 <> Index4 And _
Index7 <> Index5 And _
Index7 <> Index6 Then
SubIndex7(intMaxVal, intArray, Index1, Index2, _
Index3, Index4, Index5, Index6, Index7, _
dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex7(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, ByRef Index5, _
ByRef Index6, ByRef Index7, _
ByRef dblCount1, ByRef dblCount2)
For Index8 As Integer = 1 To 9
If Index8 <> Index1 And _
Index8 <> Index2 And _
Index8 <> Index3 And _
Index8 <> Index4 And _
Index8 <> Index5 And _
Index8 <> Index6 And _
Index8 <> Index7 Then
SubIndex8(intMaxVal, intArray, _
Index1, Index2, Index3, Index4, Index5, _
Index6, Index7, Index8, dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex8(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, ByRef Index5, _
ByRef Index6, ByRef Index7, ByRef Index8, _
ByRef dblCount1, ByRef dblCount2)
For Index9 As Integer = 1 To 9
If Index9 <> Index1 And _
Index9 <> Index2 And _
Index9 <> Index3 And _
Index9 <> Index4 And _
Index9 <> Index5 And _
Index9 <> Index6 And _
Index9 <> Index7 And _
Index9 <> Index8 Then
SubIndex9(intMaxVal, intArray, Index1, Index2, _
Index3, Index4, Index5, Index6, Index7, _
Index8, Index9, dblCount1, dblCount2)
End If
Next
End Sub
Sub SubIndex9(ByRef intMaxVal, ByRef intArray, ByRef Index1, _
ByRef Index2, ByRef Index3, ByRef Index4, ByRef Index5, _
ByRef Index6, ByRef Index7, ByRef Index8, ByRef Index9, _
ByRef dblCount1, ByRef dblCount2)
Dim intRow1 As Integer
Dim intRow3 As Integer
Dim intCol1 As Integer
Dim intCol3 As Integer
Dim intDiag1 As Integer
Dim intDiag2 As Integer
IntervalBreak(dblCount1, dblCount2, intArray)
intRow1 = Index1 + Index2 + Index3
intRow3 = Index7 + Index8 + Index9
intCol1 = Index1 + Index4 + Index7
intCol3 = Index3 + Index6 + Index9
intDiag1 = Index1 + Index5 + Index9
intDiag2 = Index3 + Index5 + Index7
If intRow1 <> intRow3 Or _
intRow1 <> intCol1 Or _
intRow1 <> intCol3 Or _
intRow1 <> intDiag1 Or _
intRow1 <> intDiag2 Then
Exit Sub
End If
If intRow1 > intMaxVal Then
intMaxVal = intRow1
intArray(0) = Index1
intArray(1) = Index2
intArray(2) = Index3
intArray(3) = Index4
intArray(4) = Index5
intArray(5) = Index6
intArray(6) = Index7
intArray(7) = Index8
intArray(8) = Index9
End If
End Sub
Sub IntervalBreak(ByRef dblCount1, ByRef dblCount2, _
ByRef intArray)
dblCount1 += 1
dblCount2 += 1
If dblCount1 > 49999 Then
dblCount1 = 0
Console.WriteLine("We are on combination number " & _
Str(dblCount2))
Console.WriteLine("So far, the largest array is:")
For Index77 As Integer = 0 To 8
Console.WriteLine(Str(intArray(Index77)))
Next
End If
End Sub
End Module
Edited on June 22, 2004, 11:18 am
Edited on June 22, 2004, 11:19 am
|
Posted by Penny
on 2004-06-22 11:10:30 |