Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky logarithm

Status
Not open for further replies.

sfenx

Programmer
Feb 23, 2001
81
BE
I have some troubles finding the right logarithm for my app. I have an array in which I would like to put some numbers (place-numbers). These numbers depend on the value of a variable (always an exponent of 2) that is given. The number of total places (pl) is also know.
The number before the place should be the array-index. The place should be the content of the array item.
I already tryed using a recursive function, but without success. Difficulty is that the odd numbers have to stay on top.
Code:
----2----  ----4----  ----8----  ----16----
1 (pl.01)  1 (pl.01)  1 (pl.01)  1  (pl.01)
2 (pl.08)  3 (pl.05)  5 (pl.05)  9  (pl.05)
           4 (pl.12)  3 (pl.09)  5  (pl.09)
           2 (pl.16)  7 (pl.13)  11 (pl.13)
                      8 (pl.20)  3  (pl.17)
                      4 (pl.24)  13 (pl.21)
                      6 (pl.28)  7  (pl.25)
                      2 (pl.32)  15 (pl.29)
                                 16 (pl.36)
                                 8  (pl.40)
                                 14 (pl.44)
                                 4  (pl.48)
                                 12 (pl.52)
                                 6  (pl.56)
                                 10 (pl.60)
                                 2  (pl.64)
my last attempt :

  Dim ARpos() As Integer
  Msg = "Total array items :"
  AR = InputBox(Msg)
  ReDim ARpos(AR)
  i = 0
  j = AR
  Do
    i = i + 1
    k = 2 ^ i
    If i Mod 2 = 0 Then
      ARpos(i) = Places / AR * j 'lower half
      j = j - 1
    Else
      ARpos(i) = ((Places / 2) / AR * i) - 1 'upper half
    End If
  Loop Until k = Places
  Msg = "Places : " & vbCrLf
  For i = 1 To AR
    Msg = Msg & i & " : " & ARpos(i) & vbCrLf
  Next
  MsgBox Msg
[\code]
This works fine for the 4 places column, but not for the others.
 
Hi sfenx,

I don't see it working mainly because the places variable is not set. What value should it have?

Herman
 
Thanks for responding, Herman.
The value of the places can be 2,4,8,16,32,64,...
(always 2 with exponent 1,2,3...), but there are no more columns after the '16' column, so in case of 32+ places, the column to refer to is the '16' column.
Code:
'calculate number of places
  In = InputBox("Value:")
  Dim Places As Integer
  i = 0
  k = 2 ^ i
  Do
    k = k * 2
    Places = k
  Loop Until In <= k [\code]

Sfenx
 
Hiya, I would really like to help, but I dont understand what your trying to do. In your code snipet below there are some problems though;

'calculate number of places
In = InputBox(&quot;Value:&quot;)
Dim Places As Integer
i = 0
k = 2 ^ i 'Will always = 1, 2 ^ 0 = 1
Do
k = k * 2 ' will always be 2, 1 * 2
Places = k
Loop Until In <= k ' will loop forever unless 'In' is less then or equal to 2

Please help me out by explaining what your after alittle more clearly
Thanks Collin

 
OK Collin, here we go.
I want to place seeding positions for players (a knockout system). These seeding positions will prevent that the strong players don't meet each other in the beginning. These places are predefined in a table (see above). The number of inscriptions and seeding positions normally depends on the data in my database.
The maximum seeding positions are calculated like this:
16 seeds -> 64+
8 seeds -> 32-63
4 seeds -> 16-31
2 seeds -> -16
Via the number of inscriptions, I calculate the number of places (seedings positions + normal players), which always has to be equal or higher then the latest(?) exponent of 2. (i.e. 17 inscr. -> 32 matches (2^5) - 16 inscr. -> 16 matches)
With these data I can search the right table for defining the seeding positions. The difficulty I have is that the positions in the lower half (even numbers) of the table start from the end of the table.
You were right about the code, I dropped some variables ! In the meantime I have made a few changes, so I put my entire code again.

Code:
Private Sub knockout()
  'calculating maximum number of seeding positions
  Dim Msg As String, Inschr As Integer, i As Integer, k As Integer
  Msg = &quot;Number of inscriptions :&quot;
  Inschr = InputBox(Msg, &quot;Max.RH&quot;)
  i = 3
  k = 2 ^ i
  Do
    i = i + 1
    k = k * 2
    maxRH = 2 ^ (i - 3)
  Loop Until Inschr < k Or maxRH = 16
  
  'calculating het number of places en number of byes
  Dim Places As Integer, Byes As Integer
  i = 0
  k = 2 ^ i
  Do
    i = i + 1
    k = k * 2
    Places = k
    Byes = k - Inschr
  Loop Until Inschr <= k
  
  'calculating seeding positions
  Dim RH As Byte, RHpos() As Integer, j  As Integer
  Msg = &quot;Number of seeding positions :&quot;
  RH = InputBox(Msg, &quot;Number of RH&quot;)
  If RH > maxRH Then RH = maxRH
  ReDim RHpos(RH)
  i = 0
  j = RH
  Do
    i = i + 1
    If i Mod 2 = 0 Then
      RHpos(i) = (Places / RH * j) / 2 'lower half
      j = j - 1
    Else
      RHpos(i) = (i * 2) - 1 'upper
    End If
  Loop Until i = RH
  Msg = &quot;Seeding positions :&quot; & vbCrLf
  For i = 1 To RH
    Msg = Msg & &quot;RH &quot; & i & &quot; : &quot; & RHpos(i) & vbCrLf
  Next
  MsgBox Msg

End Sub[\code]

Hope this makes it more comprehensible. 8-)
Sfenx
 
I resolved my problem and wrote the code pure mathematically. Still, isn't there a way to write this shorter or in a loop?
Code:
  RHpos(1) = 1
  RHpos(2) = Places
  If maxRH > 2 Then
    RHpos(3) = RHpos(1) + (2 ^ w)
    RHpos(4) = RHpos(2) - (2 ^ w)
  End If
  If maxRH > 4 Then
    RHpos(5) = RHpos(3) - (2 ^ (w - 1))
    RHpos(6) = RHpos(4) + (2 ^ (w - 1))
    RHpos(7) = RHpos(5) + (2 ^ w)
    RHpos(8) = RHpos(6) - (2 ^ w)
  End If
  If maxRH > 8 Then
    RHpos(9) = RHpos(7) - ((2 ^ w) + (2 ^ (w - 2)))
    RHpos(10) = RHpos(8) + ((2 ^ w) + (2 ^ (w - 2)))
    RHpos(11) = RHpos(9) + (2 ^ (w - 1))
    RHpos(12) = RHpos(10) - (2 ^ (w - 1))
    RHpos(13) = RHpos(11) + (2 ^ (w - 1))
    RHpos(14) = RHpos(12) - (2 ^ (w - 1))
    RHpos(15) = RHpos(13) + (2 ^ (w - 1))
    RHpos(16) = RHpos(14) - (2 ^ (w - 1))
  End If[\code]

Sfenx
 
Hi Sfenx,

Now I see what you're doing. But what's the point in creating the tables from code when the output is always the same?
Or am I still missing the clue?

Herman :-Q
 
Herman,

I need the these tables from code because the '16' column can vary, depending on the number of inscriptions. i.e. when there are 128 inscriptions, the number of seeding positions stay the same, but the places don't. With this code, the number of inscriptions can be infinitive.
Also, I need the same system (other tables) for defining the bye-places) :cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top