||integer square root, by Martin Guy's method
||try generalising to arbitrary base

base = 10  ||say
sqbase = base*base

square_root x 
  = loop1 x 0 one, x>0
  = 0, x=0
  = error "square_root of negative arg", otherwise
    where
    one = initone x base

||largest power of sqbase <= x
initone x base
  = last(takewhile(<=x)(iterate (*sqbase) 1))

|| The last time round, you divide by *two*, not the base.
loop1 op res one
  = res, one=0
  = res' div 2, one=1	|| extra line. Literal 0 cos it will be.
  = loop1 op' (res' div base) (one div sqbase), otherwise
    where
    (op',res') = loop2 op res one

loop2 op res one
  = loop2 (op-res-one) (res+2*one) one, op >= res+one
  = (op,res), otherwise

||something wrong here -- DT

form_pairs (a:x) (b:y) = (a,b) : form_pairs x y

testrun
  = filter is_interesting (form_pairs [1..] (map square_root [1..]))
    where
    is_interesting (num, sqrtnum)
    = True, num < sqrtnum*sqrtnum \/ num >= (sqrtnum+1)*(sqrtnum+1)
    = True, num mod 100 = 0
    = False, otherwise
