{"id":5179,"date":"2019-11-28T05:30:45","date_gmt":"2019-11-28T03:30:45","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=5179"},"modified":"2019-12-05T08:00:08","modified_gmt":"2019-12-05T06:00:08","slug":"menor-divisible-por-todos","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/menor-divisible-por-todos\/","title":{"rendered":"Menor divisible por todos"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   menorDivisible :: Integer -> Integer -> Integer\n<\/pre>\n<p>tal que (menorDivisible a b) es el menor n\u00famero divisible por todos los n\u00fameros desde a hasta b, ambos inclusive. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   menorDivisible 1 10                        ==  2520\n   length (show (menorDivisible 1 (3*10^5)))  ==  130141\n<\/pre>\n<p><strong>Nota<\/strong>: Este ejercicio est\u00e1 basado en el <a href=\"https:\/\/projecteuler.net\/problem=5\">problema 5<\/a> del <a href=\"https:\/\/projecteuler.net\">Proyecto Euler<\/a><\/p>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (foldl')\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible :: Integer -> Integer -> Integer\nmenorDivisible a b =\n  head [x | x <- [b..]\n          , and [x `mod` y == 0 | y <- [a..b]]]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible2 :: Integer -> Integer -> Integer\nmenorDivisible2 a b  \n  | a == b    = a\n  | otherwise = lcm a (menorDivisible (a+1) b)\n\n-- 3\u00aa soluci\u00f3n\n-- ============\n\nmenorDivisible3 :: Integer -> Integer -> Integer\nmenorDivisible3 a b = foldl lcm 1 [a..b] \n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible4 :: Integer -> Integer -> Integer\nmenorDivisible4 a b = foldl1 lcm [a..b] \n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible5 :: Integer -> Integer -> Integer\nmenorDivisible5 a b = foldl' lcm 1 [a..b] \n\n-- 6\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible6 :: Integer -> Integer -> Integer\nmenorDivisible6 a b = foldr1 lcm [a..b] \n\n-- 7\u00aa soluci\u00f3n\n-- ===========\n\nmenorDivisible7 :: Integer -> Integer -> Integer\nmenorDivisible7 a = foldr1 lcm . enumFromTo a\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--   \u03bb> menorDivisible 1 17\n--   12252240\n--   (18.63 secs, 15,789,475,488 bytes)\n--   \u03bb> menorDivisible2 1 17\n--   12252240\n--   (13.29 secs, 11,868,764,272 bytes)\n--   \u03bb> menorDivisible3 1 17\n--   12252240\n--   (0.00 secs, 114,688 bytes)\n--   \u03bb> menorDivisible4 1 17\n--   12252240\n--   (0.01 secs, 114,752 bytes)\n--   \u03bb> menorDivisible5 1 17\n--   12252240\n--   (0.01 secs, 110,640 bytes)\n--   \u03bb> menorDivisible6 1 17\n--   12252240\n--   (0.01 secs, 114,752 bytes)\n--   \u03bb> menorDivisible7 1 17\n--   12252240\n--   (0.00 secs, 110,912 bytes)\n--   \n--   \u03bb> length (show (menorDivisible3 1 (10^5)))\n--   43452\n--   (1.54 secs, 2,021,887,000 bytes)\n--   \u03bb> length (show (menorDivisible4 1 (10^5)))\n--   43452\n--   (1.47 secs, 2,021,886,616 bytes)\n--   \u03bb> length (show (menorDivisible5 1 (10^5)))\n--   43452\n--   (0.65 secs, 2,009,595,568 bytes)\n--   \u03bb> length (show (menorDivisible6 1 (10^5)))\n--   43452\n--   (0.30 secs, 172,986,840 bytes)\n--   \u03bb> length (show (menorDivisible7 1 (10^5)))\n--   43452\n--   (0.30 secs, 172,986,920 bytes)\n--   \n--   \u03bb> length (show (menorDivisible5 1 (2*10^5)))\n--   86871\n--   (2.47 secs, 7,989,147,304 bytes)\n--   \u03bb> length (show (menorDivisible6 1 (2*10^5)))\n--   86871\n--   (0.89 secs, 533,876,496 bytes)\n--   \u03bb> length (show (menorDivisible7 1 (2*10^5)))\n--   86871\n--   (0.88 secs, 533,875,608 bytes)\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nSer\u00e1 el peor de los malos<br \/>\nbrib\u00f3n que olvide<br \/>\nsu vocaci\u00f3n de diablo.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n menorDivisible :: Integer -> Integer -> Integer tal que (menorDivisible a b) es el menor n\u00famero divisible por todos los n\u00fameros desde a hasta b, ambos inclusive. Por ejemplo, menorDivisible 1 10 == 2520 length (show (menorDivisible 1 (3*10^5))) == 130141 Nota: Este ejercicio est\u00e1 basado en el problema 5 del Proyecto&#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":[100,8,438,481,185,359,243,71,288,89,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\/5179"}],"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=5179"}],"version-history":[{"count":6,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5179\/revisions"}],"predecessor-version":[{"id":5219,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5179\/revisions\/5219"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=5179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=5179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=5179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}