Ir al contenido principal

Soluciones de de un sistema

Definir la función

soluciones :: [(Integer,Integer,Integer)]

tal que sus elementos son las ternas (x,y,k) de soluciones del sistema \[x² = y³ = k\] Por ejemplo,

λ> take 6 soluciones
[(0,0,0),(-1,1,1),(1,1,1),(-8,4,64),(8,4,64),(-27,9,729)]
λ> soluciones !! (6*10^5+6)
(27000810008100027,90001800009,729043741093514580109350437400729)

Soluciones

soluciones :: [(Integer,Integer,Integer)]
soluciones =
  (0,0,0) : [(x,y,k) | k <- [n^6 | n <- [1..]]
                     , let Just x' = raiz 2 k
                     , let Just y  = raiz 3 k
                     , x <- [-x',x']]

-- (raiz n x) es es justo la raíz n-ésima del número natural x, si x es
-- una potencia n-ésima y Nothing en caso contrario. Por ejemplo,
--    raiz 2 16   ==  Just 4
--    raiz 3 216  ==  Just 6
--    raiz 5 216  ==  Nothing
raiz :: Int -> Integer -> Maybe Integer
raiz _ 1 = Just 1
raiz n x = aux (0,x)
    where aux (a,b) | d == x    = Just c
                    | c == a    = Nothing
                    | d < x     = aux (c,b)
                    | otherwise = aux (a,c)
              where c = (a+b) `div` 2
                    d = c^n