The first version uses a formula for first element based on n, which must be a power of 3 or twice a power of 3:
DefDbl A-Z
Dim crlf$, used(25)
Private Sub Form_Load()
ChDir "C:Program Files (x86)DevStudioVBprojectslooble"
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
DoEvents
num = 2 * Int(3 ^ 11 + 0.5)
n1 = 3: n2 = 6
Do
prevn1 = n1: prevn2 = n2
n1 = n1 * 3: n2 = n2 * 3
a1 = (num / n1 - n1 + 1) / 2
a2 = (num / n2 - n2 + 1) / 2
If n1 > maxn And a1 > 0 And a1 = Int(a1) Then maxn = n1: maxa = a1
If n2 > maxn And a2 > 0 And a2 = Int(a2) Then maxn = n2: maxa = a2
Loop Until a1 < 1 And a2 < 1
Text1.Text = Text1.Text & maxn & Str(maxa) & crlf
tot = 0
For i = maxa To maxa + maxn - 1
tot = tot + i
Next
Text1.Text = Text1.Text & tot & Str(num / 2) & Str(maxn * (2 * maxa + maxn - 1) / 2) & crlf
Text1.Text = Text1.Text & crlf & " done"
End Sub
finds
486 122
177147 177147 177147
which indicates the maximum N is 486, when the arithmetic seriest begins at 122.
The three copies of 177147 indicated the calculated sum, a repetition of the calculation 3^11 and the actual sum given by adding the 486 elements, as a check.
while the second method starts with the lowest arithmetic series and increases or decreases it in order to get to 3^11:
DefDbl A-Z
Dim crlf$, used(25)
Private Sub Form_Load()
ChDir "C:Program Files (x86)DevStudioVBprojectslooble"
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
DoEvents
num = Int(3 ^ 11 + 0.5)
First = 1: Last = 1: tot = 1
Do
If tot < num Then
Last = Last + 1
tot = tot + Last
ElseIf tot > num Then
tot = tot - First
First = First + 1
Else
Exit Do
End If
Loop
Text1.Text = Text1.Text & First & Str(Last) & " " & Last - First + 1 & crlf
Text1.Text = Text1.Text & crlf & " done"
End Sub
works to find the lowest starting point, which will result in the highest N. It finds:
122 607 486
indicating the series starts at 122 and ends at 607 for N = 486.
A change to the first version will show all the arithmetic series with increment 1 that find 3^11:
n first
element
9 19679
18 9833
27 6548
54 3254
81 2147
162 1013
243 608
486 122
while the change to the second version finds more:
first last n
122 607 486
608 850 243
1013 1174 162
2147 2227 81
3254 3307 54
6548 6574 27
9833 9850 18
19679 19687 9
29522 29527 6
59048 59050 3
88573 88574 2
177147 177147 1
The reason the first didn't find the extra, is that it started searching for odd n at n=9, and for even n at n=18, on the basis that the maximum n would be higher, and it was.
Edited on October 1, 2014, 7:42 am
|
Posted by Charlie
on 2014-09-30 18:40:42 |