{"id":4348,"date":"2018-11-30T06:00:17","date_gmt":"2018-11-30T04:00:17","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4348"},"modified":"2022-03-26T12:10:09","modified_gmt":"2022-03-26T10:10:09","slug":"raiz-cubica-entera","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/raiz-cubica-entera\/","title":{"rendered":"Ra\u00edz c\u00fabica entera"},"content":{"rendered":"<p>Un n\u00famero x es un cubo si existe un y tal que x = y^3. Por ejemplo, 8 es un cubo porque 8 = 2^3.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   raizCubicaEntera :: Integer -> Maybe Integer.\n<\/pre>\n<p>tal que (raizCubicaEntera x n) es justo la ra\u00edz c\u00fabica del n\u00famero natural x, si x es un cubo y Nothing en caso contrario. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   raizCubicaEntera 8             ==  Just 2\n   raizCubicaEntera 9             ==  Nothing\n   raizCubicaEntera 27            ==  Just 3\n   raizCubicaEntera 64            ==  Just 4\n   raizCubicaEntera (2^30)        ==  Just 1024\n   raizCubicaEntera (10^9000)     ==  Just (10^3000)\n   raizCubicaEntera (5 + 10^9000) ==  Nothing\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa definici\u00f3n\n-- =============\n\nraizCubicaEntera :: Integer -> Maybe Integer\nraizCubicaEntera x = aux 0\n  where aux y | y^3 > x   = Nothing\n              | y^3 == x  = Just y\n              | otherwise = aux (y+1)\n\n-- 2\u00aa definici\u00f3n\n-- =============\n\nraizCubicaEntera2 :: Integer -> Maybe Integer\nraizCubicaEntera2 x \n  | y^3 == x  = Just y\n  | otherwise = Nothing\n  where (y:_) = dropWhile (\\y -> y^3 < x) [0..]\n\n-- 3\u00aa definici\u00f3n\n-- =============\n\nraizCubicaEntera3 :: Integer -> Maybe Integer \nraizCubicaEntera3 1 = Just 1\nraizCubicaEntera3 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^3\n\n-- 4\u00aa definici\u00f3n\n-- =============\n\nraizCubicaEntera4 :: Integer -> Maybe Integer\nraizCubicaEntera4 x \n  | y^3 == x  = Just y\n  | otherwise = Nothing\n  where y = floor ((fromIntegral x)**(1 \/ 3))\n\n-- Nota. La definici\u00f3n anterior falla para n\u00fameros grandes. Por ejemplo,\n--    \u03bb> raizCubicaEntera4 (2^30)\n--    Nothing\n--    \u03bb> raizCubicaEntera (2^30)\n--    Just 1024\n\n-- Equivalencia\n-- ============\n\n-- La propiedad es                        \nprop_raizCubicaEntera :: Integer -> Property\nprop_raizCubicaEntera x =\n  x >= 0 ==>\n  and [raizCubicaEntera x == f x | f <- [ raizCubicaEntera2\n                                        , raizCubicaEntera3]]\n\n-- La comprobaci\u00f3n es              \n--    \u03bb> quickCheck prop_raizCubicaEntera\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n  \n--    \u03bb> raizCubicaEntera (10^18)\n--    Just 1000000\n--    (1.80 secs, 1,496,137,192 bytes)\n--    \u03bb> raizCubicaEntera2 (10^18)\n--    Just 1000000\n--    (0.71 secs, 712,134,128 bytes)\n--    \u03bb> raizCubicaEntera3 (10^18)\n--    Just 1000000\n--    (0.01 secs, 196,424 bytes)\n--\n--    \u03bb> raizCubicaEntera2 (5^27)\n--    Just 1953125\n--    (1.42 secs, 1,390,760,920 bytes)\n--    \u03bb> raizCubicaEntera3 (5^27)\n--    Just 1953125\n--    (0.00 secs, 195,888 bytes)\n--\n--    \u03bb> raizCubicaEntera3 (10^9000) == Just (10^3000)\n--    True\n--    (2.05 secs, 420,941,368 bytes)\n--    \u03bb> raizCubicaEntera3 (5 + 10^9000) == Nothing\n--    True\n--    (2.08 secs, 420,957,576 bytes)\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nTras el vivir y el so\u00f1ar,<br \/>\nest\u00e1 lo que m\u00e1s importa:<br \/>\ndespertar.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Un n\u00famero x es un cubo si existe un y tal que x = y^3. Por ejemplo, 8 es un cubo porque 8 = 2^3. Definir la funci\u00f3n raizCubicaEntera :: Integer -> Maybe Integer. tal que (raizCubicaEntera x n) es justo la ra\u00edz c\u00fabica del n\u00famero natural x, si x es un cubo y Nothing&#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":[7],"tags":[500,30,59,11,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4348"}],"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=4348"}],"version-history":[{"count":6,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4348\/revisions"}],"predecessor-version":[{"id":4589,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4348\/revisions\/4589"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}