Given a clock, rearrange six consecutive numbers on its face, so
the sum of every pair of adjacent numbers is a prime.
Solution #1: 12-1-2-3-4-7-10-9-8-5-6-11
Solution #2: 12-1-2-3-4-9-10-7-6-5-8-11
The following Visual Basic program found these results in less than a second.
Imports System
Imports System.IO
Imports System.Text
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 intclock(12) As Integer
strstarttime = TimeOfDay
Console.WriteLine("Start of execution: " & strstarttime)
intclock(0) = 12
intclock(1) = 1
intclock(2) = 2
intclock(3) = 3
intclock(4) = 4
intclock(5) = 5
intclock(6) = 6
intclock(7) = 7
intclock(8) = 8
intclock(9) = 9
intclock(10) = 10
intclock(11) = 11
For indexa As Integer = 0 To 11
sixnumbers(indexa, intclock)
Next
strendtime = TimeOfDay
Console.WriteLine(" ")
Console.WriteLine("Start of execution: " & strstarttime)
strendtime = TimeOfDay
Console.WriteLine("End of execution: " & strendtime)
strend = "?"
Console.WriteLine(" ")
While strend <> "X"
Console.WriteLine("Please enter X to exit program...")
strend = UCase(Console.ReadLine())
End While
End Sub
Sub sixnumbers(ByRef indexa, ByRef intclock)
Dim intsub1 As Integer
Dim intsub2 As Integer
Dim int6hours(6) As Integer
intsub1 = 0
For indexb As Integer = indexa To indexa + 5
intsub2 = indexb Mod 12
int6hours(intsub1) = intclock(intsub2)
intsub1 += 1
Next
For index1 As Integer = 0 To 5
For index2 As Integer = 0 To 5
If index2 <> index1 Then
For index3 As Integer = 0 To 5
If index3 <> index1 And _
index3 <> index2 Then
For index4 As Integer = 0 To 5
If index4 <> index1 And _
index4 <> index2 And _
index4 <> index3 Then
For index5 As Integer = 0 To 5
If index5 <> index1 And _
index5 <> index2 And _
index5 <> index3 And _
index5 <> index4 Then
For index6 As Integer = 0 To 5
If index6 <> index1 And _
index6 <> index2 And _
index6 <> index3 And _
index6 <> index4 And _
index6 <> index5 Then
adjustclock(indexa, _
index1, _
index2, _
index3, _
index4, _
index5, _
index6, _
intclock, _
int6hours)
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
Next
End Sub
Sub adjustclock(ByRef indexa, _
ByRef index1, _
ByRef index2, _
ByRef index3, _
ByRef index4, _
ByRef index5, _
ByRef index6, _
ByRef intclock, _
ByRef int6hours)
Dim intnewclock(12) As Integer
Dim intnew6hours(6) As Integer
Dim intsub1 As Integer
Dim intsub2 As Integer
Dim intsum As Integer
Dim strprime As String
For indexb As Integer = 0 To 11
intnewclock(indexb) = intclock(indexb)
Next
intnew6hours(0) = int6hours(index1)
intnew6hours(1) = int6hours(index2)
intnew6hours(2) = int6hours(index3)
intnew6hours(3) = int6hours(index4)
intnew6hours(4) = int6hours(index5)
intnew6hours(5) = int6hours(index6)
intsub1 = indexa
For indexb As Integer = 0 To 5
intnewclock(intsub1) = intnew6hours(indexb)
intsub1 += 1
intsub1 = intsub1 Mod 12
Next
strprime = "y"
For indexb As Integer = 0 To 11
intsub2 = indexb + 1
intsub2 = intsub2 Mod 12
intsum = intnewclock(indexb) + intnewclock(intsub2)
If intsum <> 3 And intsum <> 5 And intsum <> 7 And _
intsum <> 11 And intsum <> 13 And intsum <> 17 And _
intsum <> 19 And intsum <> 23 Then
strprime = "n"
Exit For
End If
Next
If strprime = "y" Then
Console.WriteLine("Here is such a clock:")
For indexb As Integer = 0 To 11
Console.WriteLine( _
Str(intnewclock(indexb)))
Next
End If
End Sub
End Module
|
Posted by Penny
on 2004-08-01 14:30:36 |