{"id":5855,"date":"2020-05-06T10:00:50","date_gmt":"2020-05-06T08:00:50","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=5855"},"modified":"2020-05-13T08:38:05","modified_gmt":"2020-05-13T06:38:05","slug":"la-sucesion-de-sylvester","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/la-sucesion-de-sylvester\/","title":{"rendered":"La sucesi\u00f3n de Sylvester"},"content":{"rendered":"<p>La sucesi\u00f3n de Sylvester es la sucesi\u00f3n que comienza en 2 y sus restantes t\u00e9rminos se obtienen multiplicando los anteriores y sum\u00e1ndole 1.<\/p>\n<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   sylvester        :: Integer -> Integer\n   graficaSylvester :: Integer -> Integer -> IO ()\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>(sylvester n) es el n-\u00e9simo t\u00e9rmino de la sucesi\u00f3n de Sylvester. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> [sylvester n | n <- [0..7]]\n     [2,3,7,43,1807,3263443,10650056950807,113423713055421844361000443]\n     \u03bb> length (show (sylvester 25))\n     6830085\n<\/pre>\n<ul>\n<li>(graficaSylvester d n) dibuja la gr\u00e1fica de los d \u00faltimos d\u00edgitos de los n primeros t\u00e9rminos de la sucesi\u00f3n de Sylvester. Por ejemplo,\n<ul>\n<li>(graficaSylvester 3 30) dibuja<br \/>\n<a href=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png?resize=640%2C480\" alt=\"La_sucesion_de_Sylvester_(3,30)\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-3879\" srcset=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png?resize=100%2C75&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_330.png?resize=150%2C112&amp;ssl=1 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/li>\n<li>(graficaSylvester 4 30) dibuja<br \/>\n<a href=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png?resize=640%2C480\" alt=\"La_sucesion_de_Sylvester_(4,30)\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-3880\" srcset=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png?resize=100%2C75&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_430.png?resize=150%2C112&amp;ssl=1 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/li>\n<li>(graficaSylvester 5 30) dibuja<br \/>\n<a href=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png?resize=640%2C480\" alt=\"La_sucesion_de_Sylvester_(5,30)\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-3881\" srcset=\"https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png?resize=100%2C75&amp;ssl=1 100w, https:\/\/i0.wp.com\/www.glc.us.es\/~jalonso\/exercitium\/wp-content\/uploads\/2018\/03\/La_sucesion_de_Sylvester_530.png?resize=150%2C112&amp;ssl=1 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Nota<\/strong>: Se puede usar programaci\u00f3n din\u00e1mica para aumentar la eficiencia.<\/p>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List               (genericIndex)\nimport Data.Array              ((!), array)\nimport Graphics.Gnuplot.Simple (plotList, Attribute (Key, PNG))\n\n-- 1\u00aa soluci\u00f3n (por recursi\u00f3n)\n-- ===========================\n\nsylvester1 :: Integer -> Integer\nsylvester1 0 = 2\nsylvester1 n = 1 + product [sylvester1 k | k <- [0..n-1]]\n\n-- 2\u00aa soluci\u00f3n (con programaci\u00f3n din\u00e1mica)\n-- =======================================\n\nsylvester2 :: Integer -> Integer\nsylvester2 n = v ! n where\n  v = array (0,n) [(i,f i) | i <- [0..n]]\n  f 0 = 2\n  f m = 1 + product [v!k | k <- [0..m-1]]\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\n-- Observando que\n--    S(n) = 1 + S(0)*S(1)*...*S(n-2)*S(n-1)\n--         = 1 + (1 + S(0)*S(1)*...*S(n-2))*S(n-1) - S(n-1)\n--         = 1 + S(n-1)*S(n-1) - S(n-1)\n--         = 1 + S(n-1)^2 - S(n-1)\n-- se obtiene la siguiente definici\u00f3n.\nsylvester3 :: Integer -> Integer\nsylvester3 0 = 2\nsylvester3 n = 1 + x^2 - x\n  where x = sylvester3 (n-1)\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nsylvester4 :: Integer -> Integer\nsylvester4 n = v ! n where\n  v = array (0,n) [(i,f i) | i <- [0..n]]\n  f 0 = 2\n  f m = 1 + x^2 - x\n    where x = v ! (m-1)\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nsylvester5 :: Integer -> Integer\nsylvester5 n = sucSylvester5 `genericIndex` n\n\nsucSylvester5 :: [Integer]\nsucSylvester5 = iterate (\\x -> (x-1)*x+1) 2 \n\n-- La comparaci\u00f3n es\n--    \u03bb> length (show (sylvester1 23))\n--    1707522\n--    (6.03 secs, 4,090,415,704 bytes)\n--    \u03bb> length (show (sylvester2 23))\n--    1707522\n--    (0.33 secs, 109,477,296 bytes)\n--    \u03bb> length (show (sylvester3 23))\n--    1707522\n--    (0.35 secs, 109,395,136 bytes)\n--    \u03bb> length (show (sylvester4 23))\n--    1707522\n--    (0.33 secs, 109,402,440 bytes)\n--    \u03bb> length (show (sylvester5 23))\n--    1707522\n--    (0.30 secs, 108,676,256 bytes)\n\ngraficaSylvester :: Integer -> Integer -> IO ()\ngraficaSylvester d n =\n  plotList [ Key Nothing\n           , PNG (\"La_sucesion_de_Sylvester_\" ++ show (d,n) ++ \".png\")\n           ]\n           [sylvester5 k `mod` (10^d) | k <- [0..n]]\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>La sucesi\u00f3n de Sylvester es la sucesi\u00f3n que comienza en 2 y sus restantes t\u00e9rminos se obtienen multiplicando los anteriores y sum\u00e1ndole 1. Definir las funciones sylvester :: Integer -> Integer graficaSylvester :: Integer -> Integer -> IO () tales que (sylvester n) es el n-\u00e9simo t\u00e9rmino de la sucesi\u00f3n de Sylvester. Por ejemplo, \u03bb>&#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":[250,8,286,256,376,10,42,89,11,309,157,6,252,33],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5855"}],"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=5855"}],"version-history":[{"count":8,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5855\/revisions"}],"predecessor-version":[{"id":5892,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5855\/revisions\/5892"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=5855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=5855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=5855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}