{"id":8545,"date":"2024-04-29T14:06:53","date_gmt":"2024-04-29T12:06:53","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=8545"},"modified":"2024-05-02T14:07:47","modified_gmt":"2024-05-02T12:07:47","slug":"29-abr-24","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/29-abr-24\/","title":{"rendered":"N\u00fameros triangulares con n cifras distintas"},"content":{"rendered":"<p>Los n\u00fameros triangulares se forman como sigue<\/p>\n<pre lang=\"haskell\">\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=\"haskell\">\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=\"haskell\">\n   triangularesConCifras :: Int -> [Integer]\n<\/pre>\n<p>tal que <code>triangularesConCifras n<\/code> es la lista de los n\u00fameros triangulares con <code>n<\/code> cifras distintas. Por  ejemplo,<\/p>\n<pre lang=\"haskell\">\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<p><!--more--><\/p>\n<h2>1. Soluciones en Haskell<\/h2>\n<pre lang=\"haskell\">\nimport Data.List (nub)\nimport Test.Hspec (Spec, describe, hspec, it, shouldBe)\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-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspecG :: (Int -> [Integer]) -> Spec\nspecG triangularesConCifras = do\n  it \"e1\" $\n    take 6 (triangularesConCifras 1) `shouldBe` [1,3,6,55,66,666]\n  it \"e2\" $\n    take 6 (triangularesConCifras 2) `shouldBe` [10,15,21,28,36,45]\n  it \"e3\" $\n    take 6 (triangularesConCifras 3) `shouldBe` [105,120,136,153,190,210]\n  it \"e4\" $\n    take 5 (triangularesConCifras 4) `shouldBe` [1035,1275,1326,1378,1485]\n\nspec :: Spec\nspec = do\n  describe \"def. 1\" $ specG triangularesConCifras1\n  describe \"def. 2\" $ specG triangularesConCifras2\n  describe \"def. 3\" $ specG triangularesConCifras3\n  describe \"def. 4\" $ specG triangularesConCifras4\n  describe \"def. 5\" $ specG triangularesConCifras5\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--    20 examples, 0 failures\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<h2>2. Soluciones en Python<\/h2>\n<pre lang=\"python\">\nfrom itertools import count, islice\nfrom sys import setrecursionlimit\nfrom timeit import Timer, default_timer\nfrom typing import Iterator\n\nsetrecursionlimit(10**6)\n\n# 1\u00aa soluci\u00f3n\n# ===========\n\n# triangular(n) es el n-\u00e9simo n\u00famero triangular. Por ejemplo,\n#    triangular(9) == 45\ndef triangular(n: int) -> int:\n    if n == 1:\n        return 1\n    return triangular(n-1) + n\n\n# triangulares1() es la lista de los n\u00fameros triangulares. Por ejemplo,\n#    >>> list(islice(triangulares1(), 10))\n#    [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]\ndef triangulares1() -> Iterator[int]:\n    return (triangular(n) for n in count(1))\n\n# nCifras(x) es el n\u00famero de cifras distintas del n\u00famero x. Por\n# ejemplo,\n#    nCifras(325275)  ==  4\ndef nCifras(x: int) -> int:\n    return len(set(str(x)))\n\ndef triangularesConCifras1(n: int) -> Iterator[int]:\n    return (x for x in triangulares1() if nCifras(x) == n)\n\n# 2\u00aa soluci\u00f3n\n# ===========\n\ndef triangulares2() -> Iterator[int]:\n    return ((n*(n+1)) \/\/ 2 for n in count(1))\n\ndef triangularesConCifras2(n: int) -> Iterator[int]:\n    return (x for x in triangulares2() if nCifras(x) == n)\n\n# 3\u00aa soluci\u00f3n\n# ===========\n\ndef triangulares3() -> Iterator[int]:\n    x = 0\n    for n in count(1):\n        x += n\n        yield x\n\ndef triangularesConCifras3(n: int) -> Iterator[int]:\n    return (x for x in triangulares3() if nCifras(x) == n)\n\n# Verificaci\u00f3n\n# ============\n\ndef test_triangularesConCifras() -> None:\n    for triangularesConCifras in [triangularesConCifras1,\n                                  triangularesConCifras2,\n                                  triangularesConCifras3]:\n        assert list(islice(triangularesConCifras(1), 6)) == \\\n            [1,3,6,55,66,666]\n        assert list(islice(triangularesConCifras(2), 6)) == \\\n            [10,15,21,28,36,45]\n        assert list(islice(triangularesConCifras(3), 6)) == \\\n            [105,120,136,153,190,210]\n        assert list(islice(triangularesConCifras(4), 5)) == \\\n            [1035,1275,1326,1378,1485]\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_triangularesConCifras()\n#    Verificado\n\n# Comparaci\u00f3n de eficiencia\n# =========================\n\ndef tiempo(e: str) -> None:\n    \"\"\"Tiempo (en segundos) de evaluar la expresi\u00f3n e.\"\"\"\n    t = Timer(e, \"\", default_timer, globals()).timeit(1)\n    print(f\"{t:0.2f} segundos\")\n\n# La comparaci\u00f3n es\n#    >>> tiempo('list(islice(triangularesConCifras1(3), 300))')\n#    11.18 segundos\n#    >>> tiempo('list(islice(triangularesConCifras2(3), 300))')\n#    0.03 segundos\n#    >>> tiempo('list(islice(triangularesConCifras3(3), 300))')\n#    0.03 segundos\n#\n#    >>> tiempo('list(islice(triangularesConCifras2(3), 700))')\n#    2.19 segundos\n#    >>> tiempo('list(islice(triangularesConCifras3(3), 700))')\n#    2.01 segundos\n<\/pre>\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":"default","_kad_post_title":"default","_kad_post_layout":"default","_kad_post_sidebar_id":"","_kad_post_content_style":"default","_kad_post_vertical_padding":"default","_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":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/8545"}],"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=8545"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/8545\/revisions"}],"predecessor-version":[{"id":8546,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/8545\/revisions\/8546"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=8545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=8545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=8545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}