{"id":3166,"date":"2017-04-03T06:00:37","date_gmt":"2017-04-03T04:00:37","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3166"},"modified":"2017-04-10T08:22:26","modified_gmt":"2017-04-10T06:22:26","slug":"rotaciones-divisibles-por-4","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/rotaciones-divisibles-por-4\/","title":{"rendered":"Rotaciones divisibles por 4"},"content":{"rendered":"<p>Las rotaciones de 928160 son 928160, 281609, 816092, 160928, 609281 y 92816. De las cuales, las divisibles por 4 son 928160, 816092, 160928 y 92816.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   nRotacionesDivisibles :: Integer -> Int\n<\/pre>\n<p>tal que (nRotacionesDivisibles n) es el n\u00famero de rotaciones del n\u00famero n divisibles por 4. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   nRotacionesDivisibles 928160          ==  4\n   nRotacionesDivisibles 44              ==  2\n   nRotacionesDivisibles (1234^50000)    ==  38684\n   nRotacionesDivisibles (1234^300000)   ==  231853\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Char (digitToInt)\n\n-- 1\u00aa definici\u00f3n\n-- =============\n\nnRotacionesDivisibles :: Integer -> Int\nnRotacionesDivisibles n =\n  length [x | x <- rotacionesNumero n, x `mod` 4 == 0]\n\n\n-- (rotacionesNumero) es la lista de la rotaciones del n\u00famero n. Por\n-- ejemplo,\n--    rotacionesNumero 235  ==  [235,352,523]\nrotacionesNumero :: Integer -> [Integer]\nrotacionesNumero = map read . rotaciones . show\n\n-- (rotaciones xs) es la lista de las rotaciones obtenidas desplazando\n-- el primer elemento xs al final. Por ejemplo,\n--    rotaciones [2,3,5]  ==  [[2,3,5],[3,5,2],[5,2,3]]\nrotaciones :: [a] -> [[a]]\nrotaciones xs = take (length xs) (iterate rota xs)\n\n-- (rota xs) es la lista a\u00f1adiendo el primer elemento de xs al\n-- final. Por ejemplo, \n--    rota [3,2,5,7]  ==  [2,5,7,3]\nrota :: [a] -> [a]\nrota (x:xs) = xs ++ [x]\n\n-- 2\u00aa definici\u00f3n\n-- =============\n\nnRotacionesDivisibles2 :: Integer -> Int\nnRotacionesDivisibles2 n = \n  length [x | x <- pares n, x `mod` 4 == 0]\n\n-- (pares n) es la lista de pares de elementos consecutivos, incluyendo\n-- el \u00faltimo con el primero. Por ejemplo,\n--    pares 928160  ==  [9,92,28,81,16,60]\npares :: Integer -> [Int]\npares n =\n  read [last ns,head ns] : [read [a,b] | (a,b) <- zip ns (tail ns)]\n  where ns = show n\n\n-- 3\u00aa definici\u00f3n\n-- =============\n\nnRotacionesDivisibles3 :: Integer -> Int\nnRotacionesDivisibles3 n =\n  ( length\n  . filter (0 ==)\n  . map (`mod` 4)\n  . zipWith (\\x y -> 2*x + y) d\n  . tail\n  . (++[i])) d\n  where\n    d@(i:dn) = (map digitToInt . show) n\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> nRotacionesDivisibles (123^1500)\n--    803\n--    (8.15 secs, 7,109,852,800 bytes)\n--    \u03bb> nRotacionesDivisibles2 (123^1500)\n--    803\n--    (0.05 secs, 0 bytes)\n--    \u03bb> nRotacionesDivisibles3 (123^1500)\n--    803\n--    (0.02 secs, 0 bytes)\n--\n--    \u03bb> nRotacionesDivisibles2 (1234^50000)\n--    38684\n--    (2.24 secs, 1,160,467,472 bytes)\n--    \u03bb> nRotacionesDivisibles3 (1234^50000)\n--    38684\n--    (0.31 secs, 68,252,040 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Las rotaciones de 928160 son 928160, 281609, 816092, 160928, 609281 y 92816. De las cuales, las divisibles por 4 son 928160, 816092, 160928 y 92816. Definir la funci\u00f3n nRotacionesDivisibles :: Integer -> Int tal que (nRotacionesDivisibles n) es el n\u00famero de rotaciones del n\u00famero n divisibles por 4. Por ejemplo, nRotacionesDivisibles 928160 == 4 nRotacionesDivisibles&#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,248,38,71,50,134,28,10,89,11,95,33,45,47,9,76],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3166"}],"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=3166"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3166\/revisions"}],"predecessor-version":[{"id":3202,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3166\/revisions\/3202"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}