I am trying to "solve" the Pell's equation for a given d
: x^2 - d * y^2 = 1
, or at least I want to get the minimal x > 0
which will solve the equation. So far so good. Here is my Haskell code
minX :: Integer -> Integer
minX n | isSquare n = 1
| otherwise = minXRec [0,1,intSqrt n] [1,0,1] 0 1 (intSqrt n) n
minXRec :: [Integer] -> [Integer] -> Integer -> Integer -> Integer -> Integer -> Integer
minXRec (p0:p1:p2:x) (q0:q1:q2:y) m d a n
| p2*p2 - n*q2*q2 == 1 = p2
| minXRec [p1, p2, newA*p2+p1] [q1, q2, newA*q2+q1] newM newD newA n
where
newM = d*a-m
newD = quot (n-newM*newM) d
newA = quot (intSqrt n + newM) newD
The logic of the code should work fine, but on compiling I get
PE66.hs:28:9: parse error on input ‘where’
Which does not provide me with enough information to fix the problem.
I already tried to put this in an let .. in ..
style but I, like this, did not get it to work.
Where is my mistake?