THE PUZZLET PAGE


PUZZLET 85 - THE SOLUTION



' Puzzlet #085
' The 11th triangular number is 66. Thus
' they're both palindromes. Can you find
' more nth triangular numbers where both
' n  and the nth triangular number  make
' palindromes, up to a max of 1 million?
' By Dave Ellis.

declare
PrintHeader()

declare IsPalindrome(p: int)
declare PrintIt(p1: int, p2: int)
def nthTriNum, TriNumVal: int

openconsole
print "Searching ...": print
PrintHeader()

nthTriNum = 1
TriNumVal = 1

do
    nthTriNum = nthTriNum + 1
    triNumVal = triNumVal + nthTriNum
    if IsPalindrome (nthTriNum)       
        if IsPalindrome (triNumVal)
            PrintIt (nthTriNum, triNumVal)
        endif
    endif
until triNumVal > 1000000

print: print "No more!"
print: print "Press any key to exit ..."
do: until inkey$ <> ""
closeconsole
end   

sub  IsPalindrome(p)
' if p is a palindrome, returns 1,
' otherwise returns 0
def flag, i, j, L: int
def p$, q$, r$: string
p$ = ltrim$(str$(p))
L = len(p$)
j = int(L/2)
i = 1
flag = 1
do
    q$ = mid$(p$, i, 1)
    r$ = mid$(p$, L + 1 - i, 1)
    if q$ <> r$
        flag = 0
    endif
    i = i + 1
until flag = 0 | i > j
return flag

sub PrintHeader
print "Triangular Numbers"
print
print " Position  Number"
print
return

sub PrintIt(p1, p2)
print using ("#######", p1),
print using ("##########", p2)
return

fLOWCHART



PROGRAM NOTES

Each triangular number has its own index number. So the triangular numbers 1, 2, 3, 6, 10, 15, and 21 have the index numbers 1, 2, 3, 4, 5, 6, and 7.  A triangular number is held in variable triNumVal (that is, its actual value), while the associated index number is held in variable nthTriNum.

The program is controlled by the following lines in the main routine, numbered 1 to 9 to make a description simpler.


1
2
3
4
5
6
7
8
9
do
    nthTriNum = nthTriNum + 1
    triNumVal = triNumVal + nthTriNum
    if IsPalindrome (nthTriNum)       
        if IsPalindrome (triNumVal)
            PrintIt (nthTriNum, triNumVal)
        endif
    endif
until triNumVal > 1000000

Here is an exegesis of the code, based on the line numbers give above:

1

do. Opens the main DO loop.

2

nthTriNum = nthTriNum + 1. Increments nthTriNum, the index value.  Note that this was originally initialised to 1.

3

triNumVal = triNumVal + nthTriNum. Calculates the next triangular number's value and stores it in triNumVal. This will be linked to its own index, nthTriNum.

4

if IsPalindrome (nthTriNum). Is nthTriNum a palindrome? If yes, go to Line 5, else go to Line 8. Checked by calling function IsPalindrome().

5

if IsPalindrome (triNumVal). If nthTriNum is a palindrome, go on to check if triNumVal is also a palindrome. If yes, go to Line 6, else go to Line 7.

6

PrintIt (nthTriNum, triNumVal). The two previous lines established that both nthTriNum and  triNumVal are palindromes, so this is a valid answer - go ahead and print it out.

7

endif. Housekeeping - close the IF-ENDIF clause opened in line 5.

8

endif. Housekeeping - close the IF-ENDIF clause opened in line 4.

8

until triNumVal > 1000000 Is triNumVal greater than one million? If not, go around the loop again. If yes, quit.


As you can see from the inset screen dump, the code finds six solutions altogether (including the trivial second and third triangular numbers, plus the original example).

 

Searching ...

Triangular Numbers

 Position  Number

          2             3
          3             6
         11           66
         77       3003
       363      66066
     1111     617716

No more!

Press a key ...




Site design/maintenance: Dave Ellis E-mail me!
Last Updated: April 18th, 2004.