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

Home > Numbers
Not A Magic Square (Posted on 2004-06-19) Difficulty: 3 of 5
Place nine different digits (from 0-9) into a 3x3 grid, such that the eight sums formed by the three rows, three columns and two diagonals are consecutive numbers.

See The Solution Submitted by Brian Smith    
Rating: 4.3333 (3 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Program used | Comment 3 of 14 |
Here is the Visual Basic program I used. It got the solution in about 1 second. It must have just hit the right combination right off the bat.
 
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 strGood As String
Dim intArray(9)
Dim dblCount1 As Double = 0
Dim dblCount2 As Double = 0
strStartTime = TimeOfDay
Console.WriteLine("Start of execution: " & strStartTime)
strGood = "N"
While strGood = "N"
    FindArray(intArray, strGood, _
     dblCount1, dblCount2)
End While
Console.WriteLine("Found it !!!!!")
For Index1 As Integer = 0 To 8
    Console.WriteLine(Str(intArray(Index1)))
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 FindArray(ByRef intArray, Byref strGood, _
ByRef dblCount1, ByRef dblCount2)
IntervalBreak(dblCount1, dblCount2)
GenArray(intArray)
TestArray(intArray, strGood)
End Sub
 
Sub GenArray(ByRef intArray)
intArray(0) = Int((9 - 0 + 1) * Rnd()) + 0
For Index2 As Integer = 1 To 8
    intArray(Index2) = intArray(0)
Next
While (intArray(1) = intArray(0))
    intArray(1) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(2) = intArray(0) Or  _
    intArray(2) = intArray(1))
    intArray(2) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(3) = intArray(0) Or _
    intArray(3) = intArray(1) Or  _
    intArray(3) = intArray(2))
    intArray(3) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(4) = intArray(0) Or _
    intArray(4) = intArray(1) Or _
    intArray(4) = intArray(2) Or _
    intArray(4) = intArray(3))
    intArray(4) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(5) = intArray(0) Or _
    intArray(5) = intArray(1) Or _
    intArray(5) = intArray(2) Or _
    intArray(5) = intArray(3) Or _
    intArray(5) = intArray(4))
    intArray(5) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(6) = intArray(0) Or _
    intArray(6) = intArray(1) Or _
    intArray(6) = intArray(2) Or _
    intArray(6) = intArray(3) Or _
    intArray(6) = intArray(4) Or _
    intArray(6) = intArray(5))
    intArray(6) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(7) = intArray(0) Or _
    intArray(7) = intArray(1) Or _
    intArray(7) = intArray(2) Or _
    intArray(7) = intArray(3) Or _
    intArray(7) = intArray(4) Or _
    intArray(7) = intArray(5) Or _
    intArray(7) = intArray(6))
    intArray(7) = Int((9 - 0 + 1) * Rnd()) + 0
End While
While (intArray(8) = intArray(0) Or _
    intArray(8) = intArray(1) Or _
    intArray(8) = intArray(2) Or _
    intArray(8) = intArray(3) Or _
    intArray(8) = intArray(4) Or _
    intArray(8) = intArray(5) Or _
    intArray(8) = intArray(6) Or _
    intArray(8) = intArray(7))
    intArray(8) = Int((9 - 0 + 1) * Rnd()) + 0
End While
End Sub
 
Sub TestArray(ByRef intArray, ByRef strGood)
Dim intRow1 As Integer
Dim intRow2 As integer
Dim intRow3 As Integer
Dim intCol1 As Integer
Dim intCol2 As Integer
Dim intCol3 As Integer
Dim intDiag1 As Integer
Dim intDiag2 As Integer
Dim intMax As Integer
Dim intMin As Integer
intRow1 = intArray(0) + intArray(1) + intArray(2)
intRow2 = intArray(3) + intArray(4) + intArray(5)
intRow3 = intArray(6) + intArray(7) + intArray(8)
intCol1 = intArray(0) + intArray(3) + intArray(6)
intCol2 = intArray(1) + intArray(4) + intArray(7)
intCol3 = intArray(2) + intArray(5) + intArray(8)
intDiag1 = intArray(0) + intArray(4) + intArray(8)
intDiag2 = intArray(2) + intArray(4) + intArray(6)
If intRow1 = intRow2 Or _
    intRow1 = intRow3 Or _
    intRow1 = intCol1 Or _
    intRow1 = intCol2 Or _
    intRow1 = intCol3 Or _
    intRow1 = intDiag1 Or _
    intRow1 = intDiag2 Then
    Exit Sub
End If
If intRow2 = intRow3 Or _
    intRow2 = intCol1 Or _
    intRow2 = intCol2 Or _
    intRow2 = intCol3 Or _
    intRow2 = intDiag1 Or _
    intRow2 = intDiag2 Then
    Exit Sub
End If
If intRow3 = intCol1 Or _
    intRow3 = intCol2 Or _
    intRow3 = intCol3 Or _
    intRow3 = intDiag1 Or _
    intRow3 = intDiag2 Then
    Exit Sub
End If
If intCol1 = intCol2 Or _
    intCol1 = intCol3 Or _
    intCol1 = intDiag1 Or _
    intCol1 = intDiag2 Then
    Exit Sub
End If
If intCol2 = intCol3 Or _
    intCol2 = intDiag1 Or _
    intCol2 = intDiag2 Then
    Exit Sub
End If
If intCol3 = intDiag1 Or _
    intCol3 = intDiag2 Then
    Exit Sub
End If
If intDiag1 = intDiag2 Then
    Exit Sub
End If
If intRow1 > intRow2 Then
    intMax = intRow1
    intMin = intRow2
Else
    intMax = intRow2
    intMin = intRow1
End If
If intRow3 > intMax Then
    intMax = intRow3
End If
If intRow3 < intMin Then
    intMin = intRow3
End If
If intCol1 > intMax Then
    intMax = intCol1
End If
If intCol1 < intMin Then
    intMin = intCol1
End If
If intCol2 > intMax Then
    intMax = intCol2
End If
If intCol2 < intMin Then
    intMin = intCol2
End If
If intCol3 > intMax Then
    intMax = intCol3
End If
If intCol3 < intMin Then
    intMin = intCol3
End If
If intDiag1 > intMax Then
    intMax = intDiag1
End If
If intDiag1 < intMin Then
    intMin = intDiag1
End If
If intDiag2 > intMax Then
    intMax = intDiag2
End If
If intDiag2 < intMin Then
    intMin = intDiag2
End If
If intMax = intMin + 7 Then
    strGood = "Y"
End If
End sub
 
Sub IntervalBreak(ByRef dblCount1, Byref dblCount2)
dblCount1 += 1
dblCount2 += 1
If dblCount1 >= 49999 Then
    dblCount1 = 0
    Console.WriteLine("Now generating array number " & _
      Str(dblCount2))
End If
End Sub
End Module
 
 
 

 

Edited on June 19, 2004, 12:19 pm
  Posted by Penny on 2004-06-19 11:48:08

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information