{"id":6871,"date":"2022-04-05T06:00:59","date_gmt":"2022-04-05T04:00:59","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6871"},"modified":"2022-04-12T09:17:19","modified_gmt":"2022-04-12T07:17:19","slug":"numeros-triangulares-con-n-cifras-distintas","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/numeros-triangulares-con-n-cifras-distintas\/","title":{"rendered":"N\u00fameros triangulares con n cifras distintas"},"content":{"rendered":"<p>Los n\u00fameros triangulares se forman como sigue<\/p>\n<pre lang=\"text\">\n   *     *      *\n        * *    * *\n              * * *\n   1     3      6\n<\/pre>\n<p>La sucesi\u00f3n de los n\u00fameros triangulares se obtiene sumando los n\u00fameros naturales. As\u00ed, los 5 primeros n\u00fameros triangulares son<\/p>\n<pre lang=\"text\">\n    1 = 1\n    3 = 1 + 2\n    6 = 1 + 2 + 3\n   10 = 1 + 2 + 3 + 4\n   15 = 1 + 2 + 3 + 4 + 5\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   triangularesConCifras :: Int -> [Integer]\n<\/pre>\n<p>tal que <code>(triangulares n)<\/code> es la lista de los n\u00fameros triangulares con <code>n<\/code> cifras distintas. Por  ejemplo,<\/p>\n<pre lang=\"text\">\n   take 6 (triangularesConCifras 1)   ==  [1,3,6,55,66,666]\n   take 6 (triangularesConCifras 2)   ==  [10,15,21,28,36,45]\n   take 6 (triangularesConCifras 3)   ==  [105,120,136,153,190,210]\n   take 5 (triangularesConCifras 4)   ==  [1035,1275,1326,1378,1485]\n   take 2 (triangularesConCifras 10)  ==  [1062489753,1239845706]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (nub)\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\ntriangularesConCifras1 :: Int -> [Integer]\ntriangularesConCifras1 n =\n  [x | x <- triangulares1,\n       nCifras x == n]\n\n-- triangulares1 es la lista de los n\u00fameros triangulares. Por ejemplo,\n--    take 10 triangulares1 == [1,3,6,10,15,21,28,36,45,55]\ntriangulares1 :: [Integer]\ntriangulares1 = map triangular [1..]\n\ntriangular :: Integer -> Integer\ntriangular 1 = 1\ntriangular n = triangular (n-1) + n\n\n-- (nCifras x) es el n\u00famero de cifras distintas del n\u00famero x. Por\n-- ejemplo,\n--    nCifras 325275  ==  4\nnCifras :: Integer -> Int\nnCifras = length . nub . show\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\ntriangularesConCifras2 :: Int -> [Integer]\ntriangularesConCifras2 n =\n  [x | x <- triangulares2,\n       nCifras x == n]\n\ntriangulares2 :: [Integer]\ntriangulares2 = [(n*(n+1)) `div` 2 | n <- [1..]]\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\ntriangularesConCifras3 :: Int -> [Integer]\ntriangularesConCifras3 n =\n  [x | x <- triangulares3,\n       nCifras x == n]\n\ntriangulares3 :: [Integer]\ntriangulares3 = 1 : [x+y | (x,y) <- zip [2..] triangulares3]\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\ntriangularesConCifras4 :: Int -> [Integer]\ntriangularesConCifras4 n =\n  [x | x <- triangulares4,\n       nCifras x == n]\n\ntriangulares4 :: [Integer]\ntriangulares4 = 1 : zipWith (+) [2..] triangulares4\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\ntriangularesConCifras5 :: Int -> [Integer]\ntriangularesConCifras5 n =\n  [x | x <- triangulares5,\n       nCifras x == n]\n\ntriangulares5 :: [Integer]\ntriangulares5 = scanl (+) 1 [2..]\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La 1\u00aa propiedad es\nprop_triangularesConCifras1 :: Bool\nprop_triangularesConCifras1 =\n  [take 2 (triangularesConCifras1 n) | n <- [1..7]] ==\n  [take 2 (triangularesConCifras2 n) | n <- [1..7]]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> prop_triangularesConCifras1\n--    True\n\n-- La 2\u00aa propiedad es\nprop_triangularesConCifras2 :: Int -> Bool\nprop_triangularesConCifras2 n =\n  all (== take 5 (triangularesConCifras2 n'))\n      [take 5 (triangularesConCifras3 n'),\n       take 5 (triangularesConCifras4 n'),\n       take 5 (triangularesConCifras5 n')]\n  where n' = 1 + n `mod` 9\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_triangularesConCifras\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> (triangularesConCifras1 3) !! 220\n--    5456556\n--    (2.48 secs, 1,228,690,120 bytes)\n--    \u03bb> (triangularesConCifras2 3) !! 220\n--    5456556\n--    (0.01 secs, 4,667,288 bytes)\n--\n--    \u03bb> (triangularesConCifras2 3) !! 600\n--    500010500055\n--    (1.76 secs, 1,659,299,872 bytes)\n--    \u03bb> (triangularesConCifras3 3) !! 600\n--    500010500055\n--    (1.67 secs, 1,603,298,648 bytes)\n--    \u03bb> (triangularesConCifras4 3) !! 600\n--    500010500055\n--    (1.20 secs, 1,507,298,248 bytes)\n--    \u03bb> (triangularesConCifras5 3) !! 600\n--    500010500055\n--    (1.15 secs, 1,507,298,256 bytes)\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Triangulares_con_cifras.hs\">GitHub<\/a>.<\/p>\n<p>La elaboraci\u00f3n de las soluciones se describe en el siguiente v\u00eddeo<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/_Ic-384xp2I\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<h4>Nuevas soluciones<\/h4>\n<ul>\n<li>En los comentarios se pueden escribir nuevas soluciones.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=&quot;haskell&quot;&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Los n\u00fameros triangulares se forman como sigue * * * * * * * * * * 1 3 6 La sucesi\u00f3n de los n\u00fameros triangulares se obtiene sumando los n\u00fameros naturales. As\u00ed, los 5 primeros n\u00fameros triangulares son 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10&#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":[2],"tags":[8,498,30,28,415,10,24,11,6,78,33,521,146,9,467],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6871"}],"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=6871"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6871\/revisions"}],"predecessor-version":[{"id":6899,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6871\/revisions\/6899"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}