Determine all possible sequences of consecutive triangular numbers whose sum is precisely 2014.
Extra Challenge: A non computer program based method.
DefDbl A-Z
Dim crlf$
Private Sub Form_Load()
ChDir "C:\Program Files (x86)\DevStudio\VB\projects\flooble"
Text1.Text = ""
crlf$ = Chr(13) + Chr(10)
Form1.Visible = True
DoEvents
firstTri = 1: lastTri = 1: Sum = 1
add4firstTri = 2: add4lastTri = 2
Do
If Sum = 2014 Then
Text1.Text = Text1.Text & firstTri & Str(lastTri) & " "
Text1.Text = Text1.Text & Str(isTri(firstTri)) & Str(isTri(lastTri)) & " "
For i = isTri(firstTri) To isTri(lastTri)
Text1.Text = Text1.Text & Str(triNum(i))
Next
Text1.Text = Text1.Text & crlf
DoEvents
lastTri = lastTri + add4lastTri: Sum = Sum + lastTri: add4lastTri = add4lastTri + 1
ElseIf Sum < 2014 Then
lastTri = lastTri + add4lastTri: Sum = Sum + lastTri: add4lastTri = add4lastTri + 1
Else
Sum = Sum - firstTri
firstTri = firstTri + add4firstTri: add4firstTri = add4firstTri + 1
End If
Loop Until firstTri > 2014
Text1.Text = Text1.Text & crlf & firstTri & Str(lastTri) & " done"
End Sub
Function isTri(t)
n = Int(Sqr(t * 2))
np = n + 1
If n * np = 2 * t Then isTri = n Else isTri = 0
End Function
Function triNum(i)
triNum = i * (i + 1) / 2
End Function
finds only two sequences of triangular numbers that add to 2014:
First & Tr(#) All the triangular numbers in the sequnce.
last nos. first &
last
10 253 4 22 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253
78 276 12 23 78 91 105 120 136 153 171 190 210 231 253 276
So, the 4th through 22 triangular numbers (10 through 253) add up to 2014 as do the 12th through 23rd (78 through 276).
|
Posted by Charlie
on 2014-08-12 17:10:24 |