' Puzzlet #072.
' The sum of the 120th and 1545th trian-
' gular numbers is 1201545. Amazingly,
' the sum is also a concatenation of the
' index numbers.
' Are there any more like this? Use a 3-
' digit followed by a 4-digit index, as
' in the example.
' By Dave Ellis.
declare PrintResult()
def triNr3, triNr4, val3, val4: int
def conc, sum: int
openconsole
print "Searching ...": print
print " tri3 val3 tri4 ",
print "val4 val3+val4": print
for triNr4 = 1000 to 9999
val4 = triNr4*(triNr4 + 1)/2
for triNr3 = 100 to 999
val3 = triNr3*(triNr3 + 1)/2
conc = 10000*triNr3 + triNr4
if conc = val3 + val4
PrintResult()
endif
next triNr3
next triNr4
print: print "No more!"
print: print "Press a key ... ",
do: until inkey$ <> ""
closeconsole
end
sub PrintResult()
print " ", triNr3,
print using ("#######", val3),
print " ", triNr4, " ", val4,
print " ", conc
return
PROGRAM NOTES
3- and 4-digit triangular numbers are represented in the code by the variables triNr3 and triNr4 respectively. The actual values of 3- and 4-digit triangular numbers are represented in the code by the variables val3 and val4 respectively.
The 4-digit triangular numbers are looped through all the indexes from 1000 to 9999 (that is, 4-digit indexes as required). At each iteration, the value of the triangular number whose index is currently under investigation is calculated. This is achieved using the formula:
value = n(n + 1)/2, where n is the index number of the triangular number, and Value is the value of the triangular number whose index is n. If you're not sure about this little gem, look at the following table:
Index Number
Value
1
1
2
3
3
6
4
10
5
15
Without bothering about the maths, you can see by inspection that the value of any triangular number equals half the product of its index number and the next index number.
Within the 4-digit loop, the 3-digit triangular numbers are looped through all the indexes from 100 to 999. The same formula is again used to calculate the value of the triangular number whose index is currently under investigation.
Now the two triangular values are concatenated into variable conc. Because we want tri3 to be in front of tri4 in the concatenation, it's only necessary to multiply tri3 by 10,000 and add that to tri4 to get it right.
Finally conc is compared to the sum of val3 and val4. If they're equal, we have a result, and it's sent to PrintResult() to print it neatly to the screen.
When the program is run, it generates the inset screen.
There are thus five answers altogether.
Searching ...
tri3 val3 tri4 val4 val3+val4
120 7260 1545 1194285 1201545
150 11325 1726 1490401 1501726
244 29890 2196 2412306 2442196
700 245350 3676 6758326 7003676
769 296065 3846 7397781 7693846
No more!
Press a key ...
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.