Results 1 to 4 of 4

Thread: 1,11,21,31,111............

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    2

    1,11,21,31,111............

    Hello guys

    How do I find the n'th term of the series give below

    1,11,21,31,111,211,311,121,221,321.....

    It goes like this
    You take the first term. Append 1,2 and 3 successively. take the second term and do the same thing and go on and on.

    One of my customers wants to assign Registration Numbers for his clients in this fashion. So I have to generate the next code in VB

    Thanks for any help.

  2. #2
    wossname
    Guest
    I think the easiest thing to do is generate a load of these numbers at the start of the program.

    I just made this up in my head, I haven't tested it yet.

    VB Code:
    1. dim Series(1 to MAX) as string 'MAX must be (any multiple of 3)+1
    2. dim i as long, j as long
    3.  
    4. Series(1)="1"
    5.  
    6. For j = 2 to MAX-2 Step 3 'customise this to suit
    7. i=i+1
    8. series(j) = "1" & Series(i)
    9. series(j+1) = "2" & Series(i)
    10. series(j+2) = "3" & Series(i)
    11. Next j

    You must select an appropriate value for MAX, say you want the first 300 terms, then you must make space for the first 301 terms. In other words round UP to the nearest multiple of three and then add one.

    I think that should work, but it may need tweaking for best performance.

  3. #3
    wossname
    Guest
    Thats a bit of a weird system for numbering things isn't it?

  4. #4
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Code:
    Function Calculate(ByVal NTerm As Long) As String
        Dim X As Long
        Dim S As String
        
        S = ""
        
        For X = 1 To NTerm
            S = Increment(S)
            DoEvents    'For large numbers it will take a while...
        Next X
        
        Calculate = StrReverse(S)
        
    End Function
    
    Function Increment(S As String) As String
        
        If Right$(S, 1) = "3" Then
            S = Increment(Left$(S, Len(S) - 1)) & "1"
        Else
            If S = "" Then S = "0"
            S = Left$(S, Len(S) - 1) & (Val(Right$(S, 1)) + 1)
        End If
        
        Increment = S
        
    End Function
    Usage:

    Code:
    Dim RegNumber As String
    
    RegNumber=Calculate(123456)
    As I wrote in the code, for large numbers it will take a while for giving you the RegNumber. For the term 123456 in my PC (Duron 700) it took about 5 seconds... It's VERY slow, but it works absolutely well. Sure exists a better way...

    Regards,

    Xmas.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width