3

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?

1 Answer 1

5

In the second guard of minXRec you don't have a conditional test. You will need to change

| minXRec [p1, p2, a*p1+p0] [q1, q2, a*q1+q0] newM newD newA n

to most probably

| otherwise = minXRec [p1, p2, a*p1+p0] [q1, q2, a*q1+q0] newM newD newA n
1
  • facepalm. 3 minutes left to mark your answer as the solution :D
    – sch0rschi
    Commented Aug 25, 2015 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.