|
-
Apr 3rd, 2002, 10:28 AM
#1
Thread Starter
New Member
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.
-
Apr 3rd, 2002, 01:13 PM
#2
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:
dim Series(1 to MAX) as string 'MAX must be (any multiple of 3)+1
dim i as long, j as long
Series(1)="1"
For j = 2 to MAX-2 Step 3 'customise this to suit
i=i+1
series(j) = "1" & Series(i)
series(j+1) = "2" & Series(i)
series(j+2) = "3" & Series(i)
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.
-
Apr 3rd, 2002, 01:16 PM
#3
Thats a bit of a weird system for numbering things isn't it?
-
Apr 4th, 2002, 07:17 AM
#4
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|