THE PUZZLET PAGE


PUZZLET 89 - THE SOLUTION



' Puzzlet #089
' An AP 2, 5, 8 ... has the 54th term of
' 262.  The sum of all 54 terms is 6996.
' Both are palindromes. Is there another
' AP whose first term and difference are
' both single digits,  with both the nth
' term and the sum of n terms being pal-
' indromes, where n is not less than 50,
' the sum isn't greater than 30000,  and
' the difference between terms is great-
' er than 1?
' By Dave Ellis.

declare IsPalindrome(p: int)
declare PrintHeader()
declare PrintResult()
def a: int : 'a = first term
def d: int : 'd = difference between terms
def n: int : 'n = number of terms
def l: int : 'l = last term
def s: int : 's = sum of terms

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

for a = 1 to 9
  for d = 1 to 9
    n = 50
    do
      l = a + (n - 1)*d
      s = n*(2*a + (n - 1)*d)/2
      if IsPalindrome(l) & IsPalindrome(s)
          PrintResult()
        endif
      endif
      n = n + 1
    until s > 30000
  next d
next a

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


sub PrintHeader
print "a: first term"
print "d: difference between terms"
print "n: number of terms"
print "l: last term"
print "s: sum of terms"
print
print "a    d    n     l      s"
print
return

sub PrintResult
print a,
print using ("####", d),
print using ("######", n),
print using ("######", l),
print using ("########", s)
return

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




PROGRAM NOTES

The variables a, d, n, l, and s have the same meanings as used in problem description, and are the same as those frequently used in world of mathematics for these formulae (which you will need to solve the Puzzlet):

The last term l  in an AP, (a is the first term, n the number of terms, and d the difference between terms):

The sum of the terms s of an AP:
formula 1


Formula 2


The problem description tells us that both a, the first term in the series, and d, the difference between successive terms, are both single digits. Therefore, it's enough to work through all possible values for these two using the two nested loops:

for a = 1 to 9
  for d = 2 to 9

For each combination of values, n is initialised to 50, since we are told that must be not less than 50.

Now a DO-LOOP is established to try out various values of n for the temporarily fixed values of  a and d. For each value of n, the last term and the sum of terms to n is calculated. Both are then tested for palindromicity using function IsPalindrome().

If they both pass, we have a valid solution, and subroutine PrintResult() is invoked to send the result to the screen.

The DO-LOOP will continue execution, incrementing n's value every time, until it causes the value of s to exceed the maximum specified value of 30,000. At this point, the DO-LOOP is exited and new values of d investigated.


When you run the program, you'll see the inset printed out onto your computer's screen.

The results where the difference d is only 1 are valid, but perhaps trivial. They will be generating progressions such as
1, 2, 3, 4 . . .

 

Searching ...

a: first term
d: difference between terms
n: number of terms
l: last term
s: sum of terms

a    d     n      l        s

1    1     77     77    3003
2    1   201   202   20502
2    5     53   262    6996
3    2     75   151    5775
3    6     88   525   23232
5    1   137   141   10001
5    1   157   161   13031
5    1   177   181   16461
9    1     93   101    5115
9    3     82   252   10701

No more!

Press any key to exit ...





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