{"id":4611,"date":"2019-01-22T06:00:33","date_gmt":"2019-01-22T04:00:33","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4611"},"modified":"2019-01-29T10:22:31","modified_gmt":"2019-01-29T08:22:31","slug":"soluciones-de-x2-y3k","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/soluciones-de-x2-y3k\/","title":{"rendered":"Soluciones de x\u00b2 = y\u00b3 = k"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\"> \n   soluciones :: [(Integer,Integer,Integer)]\n<\/pre>\n<p>tal que sus elementos son las ternas (x,y,k) de soluciones del sistema x\u00b2 = y\u00b3 = k. Por ejemplo,<\/p>\n<pre lang=\"text\"> \n   \u03bb> take 6 soluciones\n   [(0,0,0),(-1,1,1),(1,1,1),(-8,4,64),(8,4,64),(-27,9,729)]\n   \u03bb> soluciones !! (6*10^5+6) \n   (27000810008100027,90001800009,729043741093514580109350437400729)\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nsoluciones :: [(Integer,Integer,Integer)]\nsoluciones = [(n^3, n^2, n^6) | n <- enteros]\n\n-- enteros es la lista ordenada de los n\u00fameros enteros. Por ejemplo,\n--    \u03bb> take 20 enteros\n--    [0,-1,1,-2,2,-3,3,-4,4,-5,5,-6,6,-7,7,-8,8,-9,9,-10]\nenteros :: [Integer]\nenteros = 0 : concat [[-x,x] | x <- [1..]]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nsoluciones2 :: [(Integer,Integer,Integer)]\nsoluciones2 = [(x^3,x^2,x^6) | x <- 0 : aux 1]\n  where aux n  = -n : n : aux (n+1)\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nsoluciones3 :: [(Integer,Integer,Integer)]\nsoluciones3 =\n  (0,0,0) : [(x,y,k) | k <- [n^6 | n <- [1..]]  \n                     , let Just x' = raiz 2 k\n                     , let Just y  = raiz 3 k\n                     , x <- [-x',x']]\n\n-- (raiz n x) es es justo la ra\u00edz n-\u00e9sima del n\u00famero natural x, si x es\n-- una potencia n-\u00e9sima y Nothing en caso contrario. Por ejemplo,\n--    raiz 2 16   ==  Just 4\n--    raiz 3 216  ==  Just 6\n--    raiz 5 216  ==  Nothing\nraiz :: Int -> Integer -> Maybe Integer \nraiz _ 1 = Just 1\nraiz n x = aux (0,x)\n    where aux (a,b) | d == x    = Just c\n                    | c == a    = Nothing\n                    | d < x     = aux (c,b)\n                    | otherwise = aux (a,c) \n              where c = (a+b) `div` 2\n                    d = c^n\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> soluciones !! (6*10^5+6)\n--    (27000810008100027,90001800009,729043741093514580109350437400729)\n--    (1.87 secs, 247,352,728 bytes)\n--    \u03bb> soluciones2 !! (6*10^5+6)\n--    (27000810008100027,90001800009,729043741093514580109350437400729)\n--    (1.44 secs, 243,012,936 bytes)\n--    \u03bb> soluciones3 !! (6*10^5+6)\n--    (27000810008100027,90001800009,729043741093514580109350437400729)\n--    (0.84 secs, 199,599,664 bytes)\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nLeyendo a Cervantes me parece comprenderlo todo.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n soluciones :: [(Integer,Integer,Integer)] tal que sus elementos son las ternas (x,y,k) de soluciones del sistema x\u00b2 = y\u00b3 = k. Por ejemplo, \u03bb> take 6 soluciones [(0,0,0),(-1,1,1),(1,1,1),(-8,4,64),(8,4,64),(-27,9,729)] \u03bb> soluciones !! (6*10^5+6) (27000810008100027,90001800009,729043741093514580109350437400729) Soluciones &#8212; 1\u00aa soluci\u00f3n &#8212; =========== soluciones :: [(Integer,Integer,Integer)] soluciones = [(n^3, n^2, n^6) | n take 20 enteros &#8211;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[8,30,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4611"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=4611"}],"version-history":[{"count":7,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4611\/revisions"}],"predecessor-version":[{"id":4667,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4611\/revisions\/4667"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}