{"id":5370,"date":"2020-01-16T05:30:52","date_gmt":"2020-01-16T03:30:52","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=5370"},"modified":"2020-01-23T06:56:48","modified_gmt":"2020-01-23T04:56:48","slug":"elementos-multiplos-de-la-longitud-de-la-lista","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/elementos-multiplos-de-la-longitud-de-la-lista\/","title":{"rendered":"Elementos m\u00faltiplos de la longitud de la lista"},"content":{"rendered":"<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   multiplosDeLaLongitud :: [Int] -> [Int]\n   multiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int]\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>(multiplosDeLaLongitud xs) es la lista de los elementos de xs que son m\u00faltiplos de la longitud de xs. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     multiplosDeLaLongitud [2,4,6,8] == [4,8]\n<\/pre>\n<ul>\n<li>(multiplosDeLaLongitudDeConsecutivos n m) es la lista de elementos de [n..n+m-1] que son m\u00faltiplos de n. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     multiplosDeLaLongitudDeConsecutivos 9 2  ==  [10]\n     multiplosDeLaLongitudDeConsecutivos 9 12 ==  [12]\n<\/pre>\n<p>Comprobar con QuickCheck si se verifican las siguientes propiedades<\/p>\n<ul>\n<li>En cualquier conjunto de m elementos consecutivos, m divide exactamente a uno de dichos elementos. En otras palabras, si n y m son enteros positivos, entonces (multiplosDeLaLongitudDeConsecutivos n m) tiene exactamente un elemento.<\/li>\n<li>Si n es un entero positivo y m >= n, entonces (multiplosDeLaLongitudDeConsecutivos n m) es igual a [m]<\/li>\n<li>Si n y n son enteros positivos y m &lt; n, entonces (multiplosDeLaLongitudDeConsecutivos n m) es igual a [m * ceiling (n&#8217; \/ m&#8217;)] donde n&#8217; y m&#8217; son las formas decimales de n y m respectivamente.<\/li>\n<\/ul>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\nmultiplosDeLaLongitud :: [Int] -> [Int]\nmultiplosDeLaLongitud xs =\n  [x | x <- xs\n     , x `mod`  n == 0]\n  where n = length xs\n\nmultiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int]\nmultiplosDeLaLongitudDeConsecutivos n m =\n  multiplosDeLaLongitud [n..n+m-1]\n\n-- La 1\u00aa propiedad es\nprop_multiplosDeLaLongitud :: Int -> Int -> Property\nprop_multiplosDeLaLongitud n m =\n  n > 0 && m > 0\n  ==>\n  length (multiplosDeLaLongitudDeConsecutivos n m) == 1\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_multiplosDeLaLongitud\n--    +++ OK, passed 100 tests.\n\n-- La 2\u00aa propiedad es\nprop_multiplosDeLaLongitud2 :: Int -> Int -> Property\nprop_multiplosDeLaLongitud2 n m =\n  n > 0 && m >= n\n  ==>\n  multiplosDeLaLongitudDeConsecutivos n m == [m]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_multiplosDeLaLongitud2\n--    +++ OK, passed 100 tests.\n\n-- La 3\u00aa propiedad es\nprop_multiplosDeLaLongitud3 :: Int -> Int -> Property\nprop_multiplosDeLaLongitud3 n m =\n  n > 0 && 0 < m &#038;&#038; m < n\n  ==>\n  multiplosDeLaLongitudDeConsecutivos n m == [m * ceiling (n' \/ m')]\n  where n' = fromIntegral n\n        m' = fromIntegral m \n  \n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_multiplosDeLaLongitud3\n--    +++ OK, passed 100 tests.\n\n-- Con las propiedades anteriores se puede redefinir multiplos\nmultiplosDeLaLongitudDeConsecutivos2 :: Int -> Int -> [Int]\nmultiplosDeLaLongitudDeConsecutivos2 n m\n  | m < n     = [m * ceiling (n' \/ m')]\n  | otherwise = [m]\n  where n' = fromIntegral n\n        m' = fromIntegral m \n\n-- Comprobaci\u00f3n de la equivalencia\nprop_equiv :: Int -> Int -> Property\nprop_equiv n m =\n  n > 0 && m > 0\n  ==>\n  multiplosDeLaLongitudDeConsecutivos  n m ==\n  multiplosDeLaLongitudDeConsecutivos2 n m\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_multiplosDeLaLongitud\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> xs1 = [multiplosDeLaLongitudDeConsecutivos n (10^3) | n <- [1..10^4]]\n--    (0.01 secs, 0 bytes)\n--    \u03bb> xs2 = [multiplosDeLaLongitudDeConsecutivos2 n (10^3) | n <- [1..10^4]]\n--    (0.01 secs, 0 bytes)\n--    \u03bb> maximum xs1\n--    [10000]\n--    (2.51 secs, 2,093,241,168 bytes)\n--    \u03bb> maximum xs2\n--    [10000]\n--    (0.02 secs, 16,742,408 bytes)\n<\/pre>\n<h4>Referencia<\/h4>\n<ul>\n<li><a href=\"http:\/\/bit.ly\/2SFULIR\">Set of successive numbers contains unique multiple<\/a> en ProofWiki.<\/li>\n<\/ul>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nPensando que no ve\u00eda<br \/>\nporque Dios no le miraba,<br \/>\ndijo Abel cuando mor\u00eda:<br \/>\nSe acab\u00f3 lo que  se daba.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Definir las funciones multiplosDeLaLongitud :: [Int] -> [Int] multiplosDeLaLongitudDeConsecutivos :: Int -> Int -> [Int] tales que (multiplosDeLaLongitud xs) es la lista de los elementos de xs que son m\u00faltiplos de la longitud de xs. Por ejemplo, multiplosDeLaLongitud [2,4,6,8] == [4,8] (multiplosDeLaLongitudDeConsecutivos n m) es la lista de elementos de [n..n+m-1] que son m\u00faltiplos de&#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,183,28,89,146],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5370"}],"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=5370"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5370\/revisions"}],"predecessor-version":[{"id":5434,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5370\/revisions\/5434"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=5370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=5370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=5370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}