PDA

View Full Version : Solved: Almost-Langford numbers



Emily
07-06-2006, 03:24 AM
Almost-Langford numbers :

Self-describing numbers: between two digits "d" there are d digits. Almost-Langford numbers have an even number of digits and no two identical pairs of digits.


Please read http://www.research.att.com/~njas/sequences/A108116 (http://www.research.att.com/~njas/sequences/A108116)


Is there any Excel solution?


Cross Post in Mr Excel http://www.mrexcel.com/board2/viewtopic.php?t=220786

Regards
Emily

mvidas
07-06-2006, 06:49 AM
Emily,

Are you just looking to return a True/False if it is an Almost-Langford number or not? Give this a try, worked on the examples I sent it:Sub TestIt()
Debug.Print IsAlmostLangford(2002)
Debug.Print IsAlmostLangford(2003)
Debug.Print IsAlmostLangford(131003)
Debug.Print IsAlmostLangford("978416154798652002")
End Sub
Function IsAlmostLangford(ByVal TheNumber) As Boolean
Dim TheNum As String, RegEx As Object, i As Long, RegC As Object
TheNum = CStr(TheNumber) 'convert to string
IsAlmostLangford = False
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.Pattern = "\d"
If Len(RegEx.Replace(TheNum, "")) > 0 Then GoTo ExitFunc
For i = 0 To 9
RegEx.Pattern = CStr(i)
If RegEx.Test(TheNum) Then
Set RegC = RegEx.Execute(TheNum)
If RegC.Count <> 2 Then GoTo ExitFunc
If RegC.Item(1).FirstIndex - RegC.Item(0).FirstIndex - 1 <> i Then GoTo ExitFunc
End If
Next
IsAlmostLangford = True
ExitFunc:
Set RegC = Nothing
Set RegEx = Nothing
End FunctionMatt

Emily
07-06-2006, 08:10 AM
mvidas

Thanks for your reply.
Not exactly True or False, I want to list all Almost-Langford number , 10 pairs of number.
00 11 22 33 44 55 66 77 88 99

Regards
Emily

mvidas
07-06-2006, 09:19 AM
I'm afraid I still don't fully understand, do you want to generate a list of all Almost-Langford numbers? Or do you want to get the numbers used if the number is an AL number?
For example, do you want to get a list of the numbers, like:
2002
131003
231213
300131
312132
420024
12132003
14130043
15120025
23121300
23421314
25121005
25320035
30023121
31213200
31413004
34003141

Or do you want the numbers used, i.e. if the number sent was 34003141, you would want 00,11,33,44 returned?

Emily
07-06-2006, 09:34 AM
mvidas,

I found a code ( I am not fully understand :bug: ) to list all AL number for 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8

Can it be code in ("vbscript.regexp") ?

Option Explicit
Sub aMain()
Dim n%, k%, arr()
Application.ScreenUpdating = False
Range("A:P").Clear
Range("A:A").NumberFormat = "@"
Range("A1") = "Answer:"
For n = 1 To 8
k = CrtA(n, arr)
Range("A65536").End(xlUp).Offset(1, 0) = "n = " & n & "," & IIf(k = 0, " No Solution", k & " Solution")
If k > 0 Then Range("A65536").End(xlUp).Offset(1, 0).Resize(k, 1) = Application.Transpose(arr)
Next n
Application.ScreenUpdating = True
End Sub
Function CrtA(ByVal n%, ByRef arr)
Dim i%, j%, k%, l%, p%, s$
l = 2 * n
s = Space(l)
k = 0
p = 1
For i = 1 To n
For j = p To l - i - 1
If Mid$(s, j, 1) = " " And Mid$(s, j + i + 1, 1) = " " Then
s = Left$(s, j - 1) & i & Mid$(s, j + 1, i) & i & Mid$(s, j + i + 2)
Exit For
End If
Next j
If j > l - i - 1 Then
i = i - 1
p = InStr(s, i) + 1
s = Replace(s, i, " ")
i = i - 1
ElseIf i = n Then
k = k + 1
ReDim Preserve arr(1 To k)
arr(k) = s
s = Replace(s, i, " ")
i = i - 1
p = InStr(s, i) + 1
s = Replace(s, i, " ")
i = i - 1
Else
p = 1
End If
If i < 0 Then Exit For
Next i
CrtA = k
End Function

mvidas
07-06-2006, 12:06 PM
Emily,

regexp (regular expressions) is just something to test patterns in a string, the way the code you've given there is written just fine. It is very impressive code, though even after looking at that and the output I still cannot figure out what you're trying to get out of all this. For example, I cannot figure out why you would want "231213" listed as a solution for 3 but not for 2 or 1.
What exactly do you mean by "10 pairs of number" and "list all AL number for 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8"?

Emily
07-06-2006, 07:53 PM
The code which I found only work for 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8, ; how about four more numbers (i.e. 0,0,9,9)?

PS
I think there is not a complete list in http://www.research.att.com/~njas/sequences/A108116 (http://www.research.att.com/~njas/sequences/A108116)

mvidas
07-07-2006, 11:04 AM
Hi Emily,

I do understand what you're asking now (I think it was the fact that you repeat each number the way you do that confused me).

I've stepped through that code probably 100 times, trying to re-write the logic used in order to include 0 and 9 in there. I've done it a couple times to get it to use 0's (like 2002) but I cannot determine a way to get them all.

Have you tried asking whoever wrote that originally? Whoever wrote that has a great grasp on code logic, it is unfortunate that it does not incorporate all 10 digits (even if it just got 0 it would be much more complete). I did write something to use brute force to try every number, unfortunately it is way too slow to be useful. After a while of running it I am only at
00
2002
131003, 231213, 300131, 312132, 420024
12132003, 14130043, 15120025, 23121300, 23421314, 25121005, 25320035, 30023121, 31213200, 31413004, 34003141, 40031413, 41312432, 45001415, 45121425, 45300435, 50012152, 51410054, 52002151, 52412154, 53002352, 53400354, 61310036, 62312136, 63001316, 63121326, 64200246
1214230043, 1312432004, 1514002542, 1613002362, 1613400364, 1615200265, 1714200247, 2342131400, 2352430054, 2362131006, 2412134003, 2452004151, 2462314136, 2562001516, 2562300536, 2612130063

I will still be working on this (still interests me), though I honestly don't think I'll be able to fix that correctly or make something comparable that works that fast. If you can get a hold of that original author they might be able to do it for you. Where did you find it?

Matt

Emily
07-07-2006, 08:06 PM
mvidas,

Thanks.
I have ask the original writer but still no answer.

Emily
07-07-2006, 08:14 PM
Please try the code from pgc01 in MrExcel

http://www.mrexcel.com/board2/viewtopic.php?t=220786