|
-
Sep 3rd, 2001, 12:06 PM
#32
Hyperactive Member
The Prime Solution...
Sub Main()
Dim lngTimer As Long
Dim dblCode As Double
Dim aFurniture(9) As String
Dim aPrimes(9) As Integer
Dim strResult As String
Dim x As Integer
Dim y As Integer
'Set up variables
aPrimes(0) = 2
aPrimes(1) = 3
aPrimes(2) = 5
aPrimes(3) = 7
aPrimes(4) = 11
aPrimes(5) = 13
aPrimes(6) = 17
aPrimes(7) = 19
aPrimes(8) = 23
aPrimes(9) = 29
aFurniture(0) = "Table" & vbCrLf
aFurniture(1) = "Chair" & vbCrLf
aFurniture(2) = "Stool" & vbCrLf
aFurniture(3) = "Cupboard" & vbCrLf
aFurniture(4) = "Wardrobe" & vbCrLf
aFurniture(5) = "Rocking Chair" & vbCrLf
aFurniture(6) = "A" & vbCrLf
aFurniture(7) = "B" & vbCrLf
aFurniture(8) = "C" & vbCrLf
aFurniture(9) = "D" & vbCrLf
dblCode = 1
'Choose 10 random items of furniture and generate code
Randomize
For x = 0 To 9
dblCode = dblCode * aPrimes(Int(10 * Rnd))
Next x
'Start timer
lngTimer = Timer
'Deconstruct dblCode
For y = 0 To 9
For x = 0 To 9
If dblCode / aPrimes(x) = Int(dblCode / aPrimes(x)) Then
dblCode = dblCode / aPrimes(x)
strResult = strResult & aFurniture(x)
End If
Next x
Next y
MsgBox "The furniture order was:" & vbCrLf & strResult & vbCrLf & "Time elapsed: " & Timer - lngTimer
End Sub
I knocked this together in 15 minutes with little thought so excuse the lack of optimisation etc...
Now, for testing I used the oldest machine I could lay my hands on - a 486 33MHz processor. This is as slow as they come. Running the program a few times shows that this method is so fast (even on 33MHz!) that time elapsed is very close to 0. We know the Timer function is not all that accurate (and I can't be bothered to stick in TickCount) so I modified the program to deconstruct the furniture code 10000 times.
10000 deconstructions takes 11.1 seconds. Meaning each deconstruction takes on average just over one-thousandth of a second. Remember this is on a serious relic of a machine. OK, so I appreciate the elapsed time might be smaller if the random selection chooses, say, ten chairs rather than drawers (due to smaller integers) but you get the idea. Maximum time I saw was 11.5 seconds.
The prime number method works. The prime number method is fast. keds, you're welcome to d/l and test this code. Stick it in a module and compile away.
And this method could probably be improved so if you guys have any ideas...
Where's your offering keds?
There are 10 types of people in the world - those that understand binary, and those that don't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|