{"id":3833,"date":"2018-03-06T06:00:05","date_gmt":"2018-03-06T04:00:05","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3833"},"modified":"2018-03-14T07:51:51","modified_gmt":"2018-03-14T05:51:51","slug":"suma-de-los-digitos-de-las-repeticiones-de-un-numero","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/suma-de-los-digitos-de-las-repeticiones-de-un-numero\/","title":{"rendered":"Suma de los d\u00edgitos de las repeticiones de un n\u00famero"},"content":{"rendered":"<p>Dados dos n\u00fameros naturales n y x, su suma reducida se obtiene a partir del n\u00famero obtenido repitiendo n veces el x sumando sus d\u00edgitos hasta obtener un n\u00famero con s\u00f3lo un d\u00edgito. Por ejemplo, si n es 3 y x es 24 las transformaciones son<\/p>\n<pre lang=\"text\">\n   242424 ==> 18 ==> 9\n<\/pre>\n<p>An\u00e1logamente, si n es 4 y x es 7988 las transformaciones son<\/p>\n<pre lang=\"text\">\n   7988798879887988 ==> 128 ==> 11 ==> 2\n<\/pre>\n<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   sumaReducidaDigitosRepeticiones :: Integer -> Integer -> Integer\n   grafica                         :: Integer -> IO ()\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>(sumaReducidaDigitosRepeticiones n x) es la suma reducida de n repeticiones de x. Por ejemplo <\/li>\n<\/ul>\n<pre lang=\"text\">\n     sumaReducidaDigitosRepeticiones 3 24                    ==  9\n     sumaReducidaDigitosRepeticiones 4 7988                  == 2\n     sumaReducidaDigitosRepeticiones (12^(10^7)) (12^(10^7)) == 9\n<\/pre>\n<ul>\n<li>(grafica n) dibuja la gr\u00e1fica de los n primeros elementos de la sucesi\u00f3n cuyo elementos k-\u00e9simo es (sumaReducidaDigitosRepeticiones k k). Por ejemplo, (grafica 50) dibuja<br \/>\n<a href=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png?resize=640%2C480\" alt=\"Suma_de_los_digitos_de_las_repeticiones_de_un_numero50\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-3836\" srcset=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png?resize=100%2C75&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/Suma_de_los_digitos_de_las_repeticiones_de_un_numero50.png?resize=150%2C112&amp;ssl=1 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/li>\n<\/ul>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (genericReplicate)\nimport Graphics.Gnuplot.Simple\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nsumaReducidaDigitosRepeticiones :: Integer -> Integer -> Integer\nsumaReducidaDigitosRepeticiones n x =\n  sumaReducidaDigitos (repeticiones n x) \n\n-- (repeticiones n x) es el n\u00famero obtenido repitiendo n veces el x. Por\n-- ejemplo, \n--    repeticiones 3 24   ==  242424\n--    repeticiones 4 325  ==  325325325325\nrepeticiones :: Integer -> Integer -> Integer\nrepeticiones n x = read (concat (genericReplicate n (show x)))\n\n-- (sumaDigitos x) es la suma de los d\u00edgitos de x. Por ejemplo,\n--    sumaDigitos 325  ==  10\nsumaDigitos :: Integer -> Integer\nsumaDigitos x | x < 10    = x\n              | otherwise = b + sumaDigitos a\n  where (a,b) = divMod x 10\n\n-- (sumaReducidaDigitos x) es el n\u00famero obtenido a partir de x\n-- reiterando la suma de los d\u00edgitos hasta obtener un n\u00famero con s\u00f3lo un\n-- d\u00edgito. Por ejemplo,\n--    sumaReducidaDigitos 24   ==  6\n--    sumaReducidaDigitos 325  ==  1\nsumaReducidaDigitos :: Integer -> Integer\nsumaReducidaDigitos x | x < 10    = x\n                      | otherwise = sumaReducidaDigitos (sumaDigitos x)\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nsumaReducidaDigitosRepeticiones2 :: Integer -> Integer -> Integer\nsumaReducidaDigitosRepeticiones2 n x =\n  sumaReducidaDigitos2 (n * sumaReducidaDigitos2 x) \n\nsumaReducidaDigitos2 :: Integer -> Integer\nsumaReducidaDigitos2 0 = 0\nsumaReducidaDigitos2 x | y == 0    = 9\n                       | otherwise = y\n  where y = x `mod` 9\n\n-- Equivalencia de las definiciones\n-- ================================\n\n-- La propiedad es\nprop_equiv :: Positive Integer -> Positive Integer -> Bool\nprop_equiv (Positive n) (Positive x) =\n  sumaReducidaDigitosRepeticiones n x == sumaReducidaDigitosRepeticiones2 n x \n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_equiv\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> sumaReducidaDigitosRepeticiones (10^4) (10^4)\n--    1\n--    (2.00 secs, 574,667,384 bytes)\n--    \u03bb> sumaReducidaDigitosRepeticiones2 (10^4) (10^4)\n--    1\n--    (0.03 secs, 141,120 bytes)\n  \n-- Gr\u00e1fica\n-- =======\n                      \ngrafica :: Integer -> IO ()\ngrafica n =\n  plotList [Key Nothing]\n           [sumaReducidaDigitosRepeticiones2 k k | k <- [1..n]]\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dados dos n\u00fameros naturales n y x, su suma reducida se obtiene a partir del n\u00famero obtenido repitiendo n veces el x sumando sus d\u00edgitos hasta obtener un n\u00famero con s\u00f3lo un d\u00edgito. Por ejemplo, si n es 3 y x es 24 las transformaciones son 242424 ==> 18 ==> 9 An\u00e1logamente, si n es&#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,12,328,175,89,95,6,33],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3833"}],"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=3833"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3833\/revisions"}],"predecessor-version":[{"id":3871,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3833\/revisions\/3871"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}