From: Jon Schoenfield (jonscho(AT)hiwaay.net) Subject: Re: A121319 Date: Tue, 24 Apr 2007 00:43:24 -0500 ========== The following Excel/VBA macro code was used to compute values for OEIS A121319 (under the assumption that A121319(n) must have at least n actual digits, so, e.g., the 14-digit number 75353432948736 cannot serve as A121319(15), even though the last 15 digits of 2^75353432948736 are 075353432948736). The approach used is based on the following: - It is easily shown that a(n) mod 100 = 36 for all n > 1. - Thus a(3) is the smallest value from the set {136, 236, 336, ...} for which a(3) mod 1000 = (2^a(3)) mod 1000; a(3) is found to be 736. - It is then easily shown that a(n) mod 1000 = 736 for all n > 2. - Thus a(4) is the smallest value from the set {1736, 2736, 3736 ...} for which a(4) mod 10000 = (2^a(4)) mod 10000; a(4) is found to be 8376 .... The code was not intended for publication, and is included only at the request of Neil Sloane, who has assured me that no one will reproach me for "inelegant programming." (I'm afraid "inelegant" would be putting it mildly!) If you find in the code below (or in the logical approach described above) any errors that actually lead to incorrect output (i.e., to the writing of incorrect values to the cells of the Excel spreadsheet), please let me know. Please accept my apology for the various things about the code that are inelegant, inefficient, etc. -- Jon E. Schoenfield ========== Option Explicit Sub Macro1() Dim lK As Long Dim lNMax As Long Dim lNLastDigitsCompute As Long Dim lNLastDigitsMatch As Long Dim strTemp As String Dim lN As Long Dim lIDigit As Long Dim lJDigit As Long Dim lI As Long Dim bLastNDigitsMatch As Boolean lNMax = 80 lNLastDigitsCompute = lNMax ReDim lDigitsTwoToPowerOf10(lNMax, lNLastDigitsCompute) As Long ReDim lDigitsTwoToKthPower(lNMax, lNLastDigitsCompute) As Long ReDim lDigitsK(lNMax, lNLastDigitsCompute) As Long Columns("A:B").Clear lDigitsTwoToPowerOf10(0, 0) = 2 ' 2^(10^0) = 2 For lN = 1 To lNMax ReDim lDigitsMultiplier(lNLastDigitsCompute) As Long For lIDigit = 0 To lNLastDigitsCompute lDigitsMultiplier(lIDigit) = lDigitsTwoToPowerOf10(lN - 1, lIDigit) Next lIDigit ReDim lDigitsProduct(lNLastDigitsCompute) As Long lDigitsProduct(0) = 1 ' init For lI = 1 To 10 ' multiply 10 times Mult lDigitsProduct(), lDigitsMultiplier(), lNLastDigitsCompute Next lI ' copy result For lIDigit = 0 To lNLastDigitsCompute lDigitsTwoToPowerOf10(lN, lIDigit) = lDigitsProduct(lIDigit) Next lIDigit Next lN lDigitsK(1, 0) = 4 ' ones digit lDigitsK(1, 1) = 1 ' tens digit lDigitsK(2, 0) = 6 ' ones digit lDigitsK(2, 1) = 3 ' tens digit For lN = 1 To 2 ReDim lDigitsProduct(lNLastDigitsCompute) As Long lDigitsProduct(0) = 1 ' init For lJDigit = 0 To lNLastDigitsCompute ReDim lDigitsMultiplier(lNLastDigitsCompute) As Long For lIDigit = 0 To lNLastDigitsCompute lDigitsMultiplier(lIDigit) = lDigitsTwoToPowerOf10(lJDigit, lIDigit) Next lIDigit For lI = 1 To lDigitsK(lN, lJDigit) Mult lDigitsProduct(), lDigitsMultiplier(), lNLastDigitsCompute Next lI Next lJDigit ' copy result For lIDigit = 0 To lNLastDigitsCompute lDigitsTwoToKthPower(lN, lIDigit) = lDigitsProduct(lIDigit) Next lIDigit Cells(lN, 1).Value = lN ' get string for K strTemp = "" For lIDigit = lNLastDigitsCompute To 0 Step -1 strTemp = strTemp & LTrim(RTrim(Str(lDigitsK(lN, lIDigit)))) If strTemp = "0" Then strTemp = "" End If Next lIDigit Cells(lN, 2).Value = "'" & strTemp Next lN For lN = 3 To lNMax ' Init K by copying previous value For lIDigit = 0 To lNLastDigitsCompute lDigitsK(lN, lIDigit) = lDigitsK(lN - 1, lIDigit) Next lIDigit ' Init 2^K by copying previous value For lIDigit = lNLastDigitsCompute To 0 Step -1 lDigitsTwoToKthPower(lN, lIDigit) = lDigitsTwoToKthPower(lN - 1, lIDigit) Next lIDigit ' Do the following loop until the last lN digits match Do bLastNDigitsMatch = True For lIDigit = lNLastDigitsCompute To 0 Step -1 If lDigitsK(lN, lIDigit) <> 0 Then ' most significant digit of K If lIDigit < lN - 1 Then ' K actually has fewer than lN digits bLastNDigitsMatch = False End If Exit For End If Next lIDigit If bLastNDigitsMatch Then For lIDigit = lN - 1 To 0 Step -1 If lDigitsK(lN, lIDigit) <> lDigitsTwoToKthPower(lN, lIDigit) Then bLastNDigitsMatch = False Exit For End If Next lIDigit End If If bLastNDigitsMatch Then Exit Do Else ' Increment K ' (E.g., at lN = 3, we're looking for a value of K whose last 3 digits ' match the last 3 digits of 2^K; the last 2 digits currently match, ' so try incrementing K by 100.) ' lDigitsK(lN, lN - 1) = lDigitsK(lN, lN - 1) + 1 lIDigit = lN - 1 Do While lDigitsK(lN, lIDigit) > 9 lDigitsK(lN, lIDigit) = lDigitsK(lN, lIDigit) - 10 lIDigit = lIDigit + 1 lDigitsK(lN, lIDigit) = lDigitsK(lN, lIDigit) + 1 Loop ' Update 2^K ReDim lDigitsProduct(lNLastDigitsCompute) As Long For lIDigit = 0 To lNLastDigitsCompute lDigitsProduct(lIDigit) = lDigitsTwoToKthPower(lN, lIDigit) Next lIDigit ReDim lDigitsMultiplier(lNLastDigitsCompute) As Long For lIDigit = 0 To lNLastDigitsCompute lDigitsMultiplier(lIDigit) = lDigitsTwoToPowerOf10(lN - 1, lIDigit) Next lIDigit Mult lDigitsProduct(), lDigitsMultiplier(), lNLastDigitsCompute ' copy result For lIDigit = 0 To lNLastDigitsCompute lDigitsTwoToKthPower(lN, lIDigit) = lDigitsProduct(lIDigit) Next lIDigit End If Loop Cells(lN, 1).Value = lN ' get string for K strTemp = "" For lIDigit = lNLastDigitsCompute To 0 Step -1 strTemp = strTemp & LTrim(RTrim(Str(lDigitsK(lN, lIDigit)))) If strTemp = "0" Then strTemp = "" End If Next lIDigit Cells(lN, 2).Value = "'" & strTemp Next lN End Sub Sub Mult(lDigitsProduct() As Long, lDigitsMultiplier() As Long, lNLastDigitsCompute As Long) Dim lIDigit As Long Dim lJDigit As Long Dim lSum As Long ReDim lDigitsTempProduct(lNLastDigitsCompute) As Long lSum = 0 For lIDigit = 0 To lNLastDigitsCompute For lJDigit = 0 To lIDigit lSum = lSum + lDigitsProduct(lIDigit - lJDigit) * lDigitsMultiplier(lJDigit) Next lJDigit lDigitsTempProduct(lIDigit) = lSum Mod 10 lSum = lSum \ 10 Next lIDigit ' Copy resulting product into the lDigitsProduct() array For lIDigit = 0 To lNLastDigitsCompute lDigitsProduct(lIDigit) = lDigitsTempProduct(lIDigit) Next lIDigit End Sub -------------------------- Email from njas(AT)research.att.com: One very good reason for making the program you used public is that enables people to check your work! So I would like to get the actual program you used, rather than a cleaned-up version. Just put a sentence at the beginning saying that the program was not intended for publication, and is included only at my request! Neil