{"id":6593,"date":"2022-02-08T05:00:14","date_gmt":"2022-02-08T03:00:14","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6593"},"modified":"2022-04-15T12:07:46","modified_gmt":"2022-04-15T10:07:46","slug":"exponente-en-la-factorizacion","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/exponente-en-la-factorizacion\/","title":{"rendered":"Exponente en la factorizaci\u00f3n"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   exponente :: Integer -> Integer -> Int\n<\/pre>\n<p>tal que (exponente x n) es el exponente de x en la factorizac\u00f3n prima de n (se supone que x > 1 y n > 0). Por ejemplo,<\/p>\n<pre lang=\"text\">\n   exponente 2 24  ==  3\n   exponente 3 24  ==  1\n   exponente 6 24  ==  0\n   exponente 7 24  ==  0\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Numbers.Primes (primeFactors)\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 ne 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-- 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>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Exponente_en_la_factorizacion.hs\">GitHub<\/a>.<\/p>\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 factorizac\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":"","_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,501,415,11,6,521],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6593"}],"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=6593"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6593\/revisions"}],"predecessor-version":[{"id":6695,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6593\/revisions\/6695"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}