############################################################################
#
#	File:     comqir.icn
#
#	Subject:  Program to create SRCF sequence database
#
#	Author:   Ralph E. Griswold
#
#	Date:     September 23, 2000
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#
#
############################################################################
#
#  Requires:  
#
############################################################################

link lists
link periodic
link xcode
link genrfncs
link factors

$include "/tmp/comqir.inc"

$define I (1 to 10)
$define L (2 to n - 1)
$define U (n + 1 to 2 * n - 1)
$define A (1 to 2 * n)

record palindrome(n, m, seq)

global pal

procedure main()
   local n, m, iter, count, jj, okay, rat
   local seqlist

   seqlist := []

   every n := (NEXP) do {
      every m := (MEXP) do {
         if not (1 <= m <= 2 * n) then next
         if not (CEXP) then next
         iter := 5
         repeat {
            pal := []
            count := 0
            okay := 1
            rat := Sqrt(n ^ 2 + m, iter)
            every jj := cfseq(rat.numer, rat.denom) do {
               count +:= 1
               if count = 1 then next	# don't examine first term
               if jj = 2 * n then {	# presumed end
                  if not lequiv(pal, lreverse(pal)) then break
                  okay := &null
                  break
                  }
               else if jj > n then break			# too big; error
               else put(pal, jj)
               }
            if \okay then {
               iter +:= 1			# back to repeat loop
               if iter > 12 then {
                  write(&errout, "iter=", iter)
                  break break 			# iteration factor too large
                  }
               next
               }
            break
            }
         if (FEXP) then put(seqlist, palindrome(n, m, pal))
         }
      }

   xencode(seqlist, &output)

end

#  COULD JUST GO TO *pal / 2 (+1) IF A PALINDROME.

procedure eq(seq1)

   if *seq1 ~= *pal then fail

   every i := 1 to *seq1 do {
      if /pal[i] then next		# don't match nulls
      else if seq1[i] ~= pal[i] then fail
      }

   return

end

procedure constant(seq)

   if *set(seq) = 1 then return seq[1]
   else fail

end