{"id":3542,"date":"2017-12-22T06:00:00","date_gmt":"2017-12-22T04:00:00","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3542"},"modified":"2018-02-19T08:45:52","modified_gmt":"2018-02-19T06:45:52","slug":"rotaciones-divisibles-por-8","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/rotaciones-divisibles-por-8\/","title":{"rendered":"Rotaciones divisibles por 8"},"content":{"rendered":"<p>Las rotaciones de 928160 son 928160, 281609, 816092, 160928, 609281 y 92816 de las que 3 son divisibles por 8 (928160, 160928 y 92816).<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   nRotacionesDivisiblesPor8 :: Integer -> Int\n<\/pre>\n<p>tal que (nRotacionesDivisiblesPor8 x) es el n\u00famero de rotaciones de x divisibles por 8. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   nRotacionesDivisiblesPor8 928160       ==  3\n   nRotacionesDivisiblesPor8 43262488612  ==  4\n   nRotacionesDivisiblesPor8 (read (take (10^4) (cycle \"248\")))  ==  6666\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\n-- 1\u00aa definici\u00f3n\n-- =============\n\nnRotacionesDivisiblesPor8 :: Integer -> Int\nnRotacionesDivisiblesPor8 x =\n  length [y | y <- rotaciones x\n            , y `mod` 8 == 0]\n\n--    rotaciones 1234  ==  [1234,2341,3412,4123]\nrotaciones :: Integer -> [Integer]\nrotaciones x = [read ys | ys <- rotacionesLista xs]\n  where xs = show x\n\n--    rotacionesLista \"abcd\"  ==  [\"abcd\",\"bcda\",\"cdab\",\"dabc\"]\nrotacionesLista :: [a] -> [[a]]\nrotacionesLista xs =\n  [zs ++ ys | k <- [0 .. length xs - 1]\n            , let (ys,zs) = splitAt k xs] \n\n-- 2\u00aa definici\u00f3n\n-- =============\n\nnRotacionesDivisiblesPor8b :: Integer -> Int\nnRotacionesDivisiblesPor8b x =\n  length [y | y <- tresDigitosConsecutivos x\n            , y `mod` 8 == 0]\n\n--    tresDigitosConsecutivos 1234  ==  [123,234,341,412]\ntresDigitosConsecutivos :: Integer -> [Integer]\ntresDigitosConsecutivos x =\n  [read (take 3 ys) | ys <- rotacionesLista (show x)]\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> nRotacionesDivisiblesPor8 (read (take (3*10^3) (cycle \"248\")))\n--    2000\n--    (3.59 secs, 4,449,162,144 bytes)\n--    \u03bb> nRotacionesDivisiblesPor8b (read (take (3*10^3) (cycle \"248\")))\n--    2000\n--    (0.48 secs, 593,670,656 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Las rotaciones de 928160 son 928160, 281609, 816092, 160928, 609281 y 92816 de las que 3 son divisibles por 8 (928160, 160928 y 92816). Definir la funci\u00f3n nRotacionesDivisiblesPor8 :: Integer -> Int tal que (nRotacionesDivisiblesPor8 x) es el n\u00famero de rotaciones de x divisibles por 8. Por ejemplo, nRotacionesDivisiblesPor8 928160 == 3 nRotacionesDivisiblesPor8 43262488612 ==&#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,28,89,95,33,73,47],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3542"}],"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=3542"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3542\/revisions"}],"predecessor-version":[{"id":3800,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3542\/revisions\/3800"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}