{"id":6238,"date":"2021-04-02T06:00:52","date_gmt":"2021-04-02T04:00:52","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6238"},"modified":"2021-04-09T10:59:54","modified_gmt":"2021-04-09T08:59:54","slug":"antiimagen-de-funcion-creciente","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/antiimagen-de-funcion-creciente\/","title":{"rendered":"Antiimagen de funci\u00f3n creciente"},"content":{"rendered":"<p>Una funci\u00f3n f de los n\u00fameros naturales en los n\u00fameros naturales es estrictamente creciente si para cualquier par de n\u00fameros x, y tales que x &lt; y se verifica que f(x) &lt; f(y). La antiimagen por f de un n\u00famero natural t es el n\u00famero natural x tal que f(x) = t. No todos los n\u00fameros tienen antiimiagen por f, pero en caso de tenerla es \u00fanica.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   antiimagen :: (Integer -> Integer) -> Integer -> Maybe Integer\n<\/pre>\n<p>tal que, suponiendo que f es una funci\u00f3n creciente de los n\u00fameros naturales en los n\u00fameros naturales, (antiimagen f t) es justamente la antiimagen por f de t, si existe y es Nothing, en caso contrario. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   antiimagen (\\x -> 2*x^2-3) 47  ==  Just 5\n   antiimagen (\\x -> 2*x^2-3) 48  ==  Nothing\n   antiimagen (\\x -> 2^x) 1024    ==  Just 10\n   antiimagen (2^) 1024           ==  Just 10\n   antiimagen (2^) (2^(10^7))     ==  Just 10000000\n   antiimagen (2^) (1+2^(10^7))   ==  Nothing\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Maybe (listToMaybe)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nantiimagen :: (Integer -> Integer) -> Integer -> Maybe Integer\nantiimagen f t =\n  listToMaybe [x | x <- [0..t], f x == t]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nantiimagen2 :: (Integer -> Integer) -> Integer -> Maybe Integer\nantiimagen2 f t = busqueda (0,t)\n  where busqueda (a,b) = listToMaybe [x | x <- [a..b], f x == t]\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nantiimagen3 :: (Integer -> Integer) -> Integer -> Maybe Integer\nantiimagen3 f t = busqueda (0,t)\n  where busqueda (a,b)\n          | a > b     = Nothing\n          | t < f m   = busqueda (a,m-1)\n          | t == f m  = Just m\n          | otherwise = busqueda (m+1,b)\n          where m = (a+b) `div` 2\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nantiimagen4 :: (Integer -> Integer) -> Integer -> Maybe Integer\nantiimagen4 f t | f x == t  = Just x\n                | otherwise = Nothing\n  where x = busqueda (cotas f t)\n        busqueda (a,b) = head [x | x <- [a+1..b], t <= f x]\n\n-- (cotas f t) es un par (a,b) de potencias consecutivas de 2 tal que la\n-- antiimagen de t por f est\u00e1 en el intevalo [a+1,b]. Por ejemplo,\n--    cotas (\\x -> 2*x^2-3) 47  ==  (4,8)\n--    cotas (\\x -> 2^x) 1024    ==  (8,16)\ncotas :: (Integer -> Integer) -> Integer -> (Integer, Integer)\ncotas f t | t <= f 0  = (-1,0)\n          | otherwise = (b `div` 2, b)\n  where b = until done (*2) 1\n        done b = t <= f b\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nantiimagen5 :: (Integer -> Integer) -> Integer -> Maybe Integer\nantiimagen5 f t | f x == t  = Just x\n                | otherwise = Nothing\n  where x = busqueda (cotas f t)\n        busqueda (a,b) | a+1 == b  = b\n                       | t <= f m  = busqueda (a,m)\n                       | otherwise = busqueda (m,b)\n          where m = (a+b) `div`  2\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> antiimagen (2^) (2^30)\n--    Just 30\n--    (0.01 secs, 142,728 bytes)\n--    \u03bb> antiimagen2 (2^) (2^30)\n--    Just 30\n--    (0.01 secs, 142,784 bytes)\n--    \u03bb> antiimagen3 (2^) (2^30)\n--    Just 30\n--    (8.05 secs, 335,820,048 bytes)\n--    \u03bb> antiimagen4 (2^) (2^30)\n--    Just 30\n--    (0.01 secs, 133,984 bytes)\n--    \u03bb> antiimagen5 (2^) (2^30)\n--    Just 30\n--    (0.02 secs, 121,816 bytes)\n--\n--    \u03bb> antiimagen (2^) (2^50000)\n--    Just 50000\n--    (1.29 secs, 673,789,160 bytes)\n--    \u03bb> antiimagen2 (2^) (2^50000)\n--    Just 50000\n--    (1.28 secs, 673,791,368 bytes)\n--    \u03bb> antiimagen4 (2^) (2^50000)\n--    Just 50000\n--    (0.84 secs, 342,311,360 bytes)\n--    \u03bb> antiimagen5 (2^) (2^50000)\n--    Just 50000\n--    (0.01 secs, 549,688 bytes)\n--\n--    \u03bb> antiimagen4 (2^) (2^100000)\n--    Just 100000\n--    (4.37 secs, 1,210,476,848 bytes)\n--    \u03bb> antiimagen5 (2^) (2^100000)\n--    Just 100000\n--    (0.01 secs, 918,096 bytes)\n<\/pre>\n<h4>Nuevas soluciones<\/h4>\n<ul>\n<li>En los comentarios se pueden escribir nuevas soluciones.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=&quot;haskell&quot;&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Una funci\u00f3n f de los n\u00fameros naturales en los n\u00fameros naturales es estrictamente creciente si para cualquier par de n\u00fameros x, y tales que x &lt; y se verifica que f(x) &lt; f(y). La antiimagen por f de un n\u00famero natural t es el n\u00famero natural x tal que f(x) = t. No todos los&#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":[2],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6238"}],"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=6238"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6238\/revisions"}],"predecessor-version":[{"id":6280,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6238\/revisions\/6280"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}