[RESOLVED] help with algorthm plz
ok i need to write a function/s which when applied to the following numbers outputs these 2 corresponding results. (if more numbers are required then plz say, but i think atleast 3 sets should be sufficent to work out a pattern
number = 4 desired output 4 and 0
number = 18 desired output 2 and 1
number = 32 desired output 0 and 2
number = 34 desired output 2 and 2
number = 47 desired output 15 and 2
number = 50 desired output 2 and 2
number = 54 desired output 6 and 2
story behind the numbers
i am working on a 16x14 grid and well i'm trying to get the correct grid coordinates. so lets say i count along 15spaces(+1 extra space for the 0) i should get to the cord 15,0 , so if i was given the number 15 i need that cord to appear.
if i count along 18 spaces(+1 extra space for the 0) i should get to the cord 2,1 (x repeating its iteration and y iteration increasing)
and well if i was given the number 18 i need that cord (2,1) to appear.
*count from left to right
Some of the grid
Code:
'Grid
'0-0, 1-0, 2-0, 3-0, 4-0, 5-0, 6-0, 7-0, 8-0, 9-0, 10-0, 11-0, 12-0, 13-0, 14-0, 15-0
'0-1, 1-1, 2-1, 3-1, 4-1, 5-1, 6-1, 7-1, 8-1, 9-1, 10-1, 11-1, 12-1, 13-1, 14-1, 15-1
'0-2, 1-2, 2-2, 3-2, 4-2, 5-2, 6-2, 7-2, 8-2, 9-2, 10-2, 12-2, 12-2, 13-2, 14-2, 15-2
'0-3, 1-3, 2-3, 3-3, 4-3, 5-3, 6-3, 7-3, 8-3, 9-3, 10-3, 12-3, 12-3, 13-3, 14-3, 15-3
Re: help with algorthm plz
That should be easy, here is some "pseudo-code" (not tested!)
Code:
Dim Entry as Integer
Dim Row as Integer
Dim Column as Integer
Dim OutputString as String
'The desired Row is calculated by devideing the entra by 16 and using the integer part of the result.
Row=Int(Entry/16)
'The column is calculated by using the Mod 16 of the entry.
Column=Entry Mod 16
OutputString= "The values are Column ("&column &" ) and Row (" & Row & " )"
Re: help with algorthm plz
Re: help with algorthm plz
Quote:
Originally Posted by opus
Row = Entry \ 16
This does the same thing, only much faster. (Just in case you need to do something like this a few million times)
Re: help with algorthm plz
Quote:
Originally Posted by Logophobic
Row = Entry \ 16
This does the same thing, only much faster. (Just in case you need to do something like this a few million times)
He didn't specify a language, therefore, the \ may not even work in his language of choice. Also I remember someone testing, maybe Merrion, and proving that doing Int(whatever / whatever) is faster than \. I may be wrong though.
Re: [RESOLVED] help with algorthm plz
Using this code, for both loops it is 1.9375, EVERY TIME.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myDate1 As Date = Date.Now
Dim x As Integer
For i As Integer = 0 To 1000000000
x = 5 \ 3
Next i
Dim totalTime As Double = (Date.Now - myDate1).TotalSeconds
MessageBox.Show(totalTime.ToString())
myDate1 = Date.Now
For i As Integer = 0 To 1000000000
x = 5 \ 3
Next i
totalTime = (Date.Now - myDate1).TotalSeconds
MessageBox.Show(totalTime.ToString())
End Sub
Using this code:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myDate1 As Date = Date.Now
Dim x As Integer
For i As Integer = 0 To 1000000000
x = 5 \ 3
Next i
Dim totalTime As Double = (Date.Now - myDate1).TotalSeconds
MessageBox.Show(totalTime.ToString())
myDate1 = Date.Now
For i As Integer = 0 To 1000000000
x = Int(5 / 3)
Next i
totalTime = (Date.Now - myDate1).TotalSeconds
MessageBox.Show(totalTime.ToString())
End Sub
In this case: The first loops gives, "1.9375"
The second loop gives, "109.625"
So, actually, casting it to an int is around 56x slower than using the backslash.