Private Sub Go(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim width As Integer
Dim lastRow, lastCol As Integer
width = txtIn.Text
Dim outputArray(width, width) As Integer
Dim x, y As Integer
For i = 1 To width
For j = 1 To width
outputArray(i, j) = 0
Next
Next
x = Int(width/2) + 1
y = 1
outputArray(x, y) = 1
For number = 2 To width ^ 2
lastRow = x
lastCol = y
If x = width Then
x = 1
Else
x += 1
End If
If y = 1 Then
y = width
Else
y -= 1
End If
If outputArray(x, y) = 0 Then
outputArray(x, y) = number
Else
If lastCol = width Then
y = 1
outputArray(lastRow, y) = number
x = lastRow
Else
y = lastCol + 1
x = lastRow
outputArray(lastRow, y) = number
End If
End If
Next
Dim s As String
For countrow = 1 To width
s += vbCrLf
For countcol = 1 To width
s += CStr(outputArray(countcol, countrow)) + vbTab
Next
Next
txtOut.Text = s
End Sub