farey (a,b) n = if r == 0
    then (k, n)
    else farey (a,b) (n - 1)
    where (k, r) = divMod (1 + a * n) b
farey (a,b) n = if r == 0
    then (k, n)
    else farey (a,b) (n - 1)
    where (k, r) = divMod (1 + a * n) b
farey (a,b) n = if r == 0
    then (k, n)
    else farey (a,b) (n - 1)
    where (k, r) = divMod (1 + a * n) b