{"id":8517,"date":"2024-03-19T06:00:35","date_gmt":"2024-03-19T04:00:35","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=8517"},"modified":"2024-03-30T18:15:36","modified_gmt":"2024-03-30T16:15:36","slug":"19-mar-24","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/19-mar-24\/","title":{"rendered":"Exponente en la factorizaci\u00f3n"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"haskell\">\n   exponente :: Integer -> Integer -> Int\n<\/pre>\n<p>tal que (exponente x n) es el exponente de x en la factorizaci\u00f3n prima de n (se supone que x > 1 y n > 0). Por ejemplo,<\/p>\n<pre lang=\"haskell\">\n   exponente 2 24  ==  3\n   exponente 3 24  ==  1\n   exponente 6 24  ==  0\n   exponente 7 24  ==  0\n<\/pre>\n<p><!--more--><\/p>\n<p><a name=\"haskell\"><\/a><\/p>\n<h2>1. Soluciones en Haskell<\/h2>\n<pre lang=\"haskell\">\nmodule Exponente_en_la_factorizacion where\n\nimport Data.Numbers.Primes (primeFactors)\nimport Test.Hspec (Spec, describe, hspec, it, shouldBe)\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nexponente1 :: Integer -> Integer -> Int\nexponente1 x n\n  | esPrimo x = aux n\n  | otherwise = 0\n  where aux m | m `mod` x == 0 = 1 + aux (m `div` x)\n              | otherwise      = 0\n\n-- (esPrimo x) se verifica si x es un n\u00famero primo. Por ejemplo,\n--    esPrimo 7  ==  True\n--    esPrimo 8  ==  False\nesPrimo :: Integer -> Bool\nesPrimo x =\n  [y | y <- [1..x], x `mod` y == 0] == [1,x]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nexponente2 :: Integer -> Integer -> Int\nexponente2 x n\n  | esPrimo x = length (takeWhile (`divisible` x) (iterate (`div` x) n))\n  | otherwise = 0\n\n-- (divisible n x) se verifica si n es divisible por x. Por ejemplo,\n--    divisible 6 2  ==  True\n--    divisible 7 2  ==  False\ndivisible :: Integer -> Integer -> Bool\ndivisible n x = n `mod` x == 0\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nexponente3 :: Integer -> Integer -> Int\nexponente3 x n =\n  length (filter (==x) (primeFactors n))\n\n-- Verificaci\u00f3n\n-- ============\n\nverifica :: IO ()\nverifica = hspec spec\n\nspecG :: (Integer -> Integer -> Int) -> Spec\nspecG exponente = do\n  it \"e1\" $\n    exponente 2 24  `shouldBe`  3\n  it \"e2\" $\n    exponente 3 24  `shouldBe`  1\n  it \"e3\" $\n    exponente 6 24  `shouldBe`  0\n  it \"e4\" $\n    exponente 7 24  `shouldBe`  0\n\nspec :: Spec\nspec = do\n  describe \"def. 1\" $ specG exponente1\n  describe \"def. 2\" $ specG exponente2\n  describe \"def. 3\" $ specG exponente3\n\n-- La verificaci\u00f3n es\n--    \u03bb> verifica\n--\n--    12 examples, 0 failures\n\n-- Equivalencia de las definiciones\n-- ================================\n\n-- La propiedad es\nprop_exponente :: Integer -> Integer -> Property\nprop_exponente x n =\n  x > 1 && n > 0 ==>\n  exponente1 x n == exponente2 x n &&\n  exponente1 x n == exponente3 x n\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_exponente\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p><a name=\"python\"><\/a><\/p>\n<h2>2. Soluciones en Python<\/h2>\n<pre lang=\"python\">\nfrom itertools import takewhile\nfrom typing import Callable, Iterator\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\nfrom sympy.ntheory import factorint\n\n# 1\u00aa soluci\u00f3n\n# ===========\n\n# esPrimo(x) se verifica si x es un n\u00famero primo. Por ejemplo,\n#    esPrimo(7)  ==  True\n#    esPrimo(8)  ==  False\ndef esPrimo(x: int) -> bool:\n    return [y for y in range(1, x+1) if x % y == 0] == [1,x]\n\ndef exponente1(x: int, n: int) -> int:\n    def aux (m: int) -> int:\n        if m % x == 0:\n            return 1 + aux(m \/\/ x)\n        return 0\n    if esPrimo(x):\n        return aux(n)\n    return 0\n\n# 2\u00aa soluci\u00f3n\n# ===========\n\n# iterate(f, x) es el iterador obtenido aplicando f a x y continuando\n# aplicando f al resultado anterior. Por ejemplo,\n#    >>> list(islice(iterate(lambda x : 4 * x, 1), 5))\n#    [1, 4, 16, 64, 256]\ndef iterate(f: Callable[[int], int], x: int) -> Iterator[int]:\n    r = x\n    while True:\n        yield r\n        r = f(r)\n\n# divisible(n, x) se verifica si n es divisible por x. Por ejemplo,\n#    divisible(6, 2)  ==  True\n#    divisible(7, 2)  ==  False\ndef divisible(n: int, x: int) -> bool:\n    return n % x == 0\n\ndef exponente2(x: int, n: int) -> int:\n    if esPrimo(x):\n        return len(list(takewhile(lambda m : divisible(m, x),\n                                  iterate(lambda m : m \/\/ x, n))))\n    return 0\n\n# 3\u00aa soluci\u00f3n\n# ===========\n\ndef exponente3(x: int, n: int) -> int:\n    return factorint(n, multiple = True).count(x)\n\n# Verificaci\u00f3n\n# ============\n\ndef test_exponente() -> None:\n    for exponente in [exponente1, exponente2, exponente3]:\n        assert exponente(2, 24) == 3\n        assert exponente(3, 24) == 1\n        assert exponente(6, 24) == 0\n        assert exponente(7, 24) == 0\n    print(\"Verificado\")\n\n# La verificaci\u00f3n es\n#    >>> test_exponente()\n#    Verificado\n\n# Equivalencia de las definiciones\n# ================================\n\n# La propiedad es\n@given(st.integers(min_value=1, max_value=1000),\n       st.integers(min_value=0, max_value=1000))\ndef test_exponente_equiv(x: int, n: int) -> None:\n    r = exponente1(x, n)\n    assert r == exponente2(x, n)\n    assert r == exponente3(x, n)\n\n# La comprobaci\u00f3n es\n#    >>> test_exponente_equiv()\n#    >>>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n exponente :: Integer -> Integer -> Int tal que (exponente x n) es el exponente de x en la factorizaci\u00f3n prima de n (se supone que x > 1 y n > 0). Por ejemplo, exponente 2 24 == 3 exponente 3 24 == 1 exponente 6 24 == 0 exponente 7&#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\/8517"}],"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=8517"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/8517\/revisions"}],"predecessor-version":[{"id":8518,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/8517\/revisions\/8518"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=8517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=8517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=8517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}