{"id":2921,"date":"2017-02-13T06:00:32","date_gmt":"2017-02-13T04:00:32","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2921"},"modified":"2017-02-20T08:25:39","modified_gmt":"2017-02-20T06:25:39","slug":"calculo-de-pi-usando-el-producto-de-wallis","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/calculo-de-pi-usando-el-producto-de-wallis\/","title":{"rendered":"C\u00e1lculo de pi usando el producto de Wallis"},"content":{"rendered":"<p>El <a href=\"http:\/\/bit.ly\/2l8Btek\">producto de Wallis<\/a> es una expresi\u00f3n, descubierta por John Wallis en 1655, para representar el valor de \u03c0 y que establece que:<\/p>\n<pre lang=\"text\">\n    \u03c0     2     2     4     4     6     6     8     8\n   --- = --- \u00b7 --- \u00b7 --- \u00b7 --- \u00b7 --- \u00b7 --- \u00b7 --- \u00b7 --- \u00b7\u00b7\u00b7\n    2     1     3     3     5     5     7     7     9\n<\/pre>\n<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   factoresWallis  :: [Rational]\n   productosWallis :: [Rational]\n   aproximacionPi  :: Int -> Double\n   errorPi         :: Double -> Int\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>factoresWallis es la sucesi\u00f3n de los factores del productos de Wallis. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> take 10 factoresWallis\n     [2 % 1,2 % 3,4 % 3,4 % 5,6 % 5,6 % 7,8 % 7,8 % 9,10 % 9,10 % 11]\n<\/pre>\n<ul>\n<li>productosWallis es la sucesi\u00f3n de los productos de los primeros factores de Wallis. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> take 7 productosWallis\n     [2 % 1,4 % 3,16 % 9,64 % 45,128 % 75,256 % 175,2048 % 1225]\n<\/pre>\n<ul>\n<li>(aproximacionPi n) es la aproximaci\u00f3n de pi obtenida multiplicando los n primeros factores de Wallis. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     aproximacionPi 20     ==  3.2137849402931895\n     aproximacionPi 200    ==  3.1493784731686008\n     aproximacionPi 2000   ==  3.142377365093878\n     aproximacionPi 20000  ==  3.141671186534396\n<\/pre>\n<ul>\n<li>(errorPi x) es el menor n\u00famero de factores de Wallis necesarios para obtener pi con un error menor que x. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     errorPi 0.1     ==  14\n     errorPi 0.01    ==  155\n     errorPi 0.001   ==  1569\n     errorPi 0.0001  ==  15707\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Ratio\n\nfactoresWallis :: [Rational]\nfactoresWallis =\n  concat [[y%(y-1),  y%(y+1)] | x <- [1..], let y = 2*x]\n\nproductosWallis :: [Rational]\nproductosWallis = scanl1 (*) factoresWallis\n\naproximacionPi :: Int -> Double\naproximacionPi n =\n  fromRational (2 * productosWallis !! n)\n\nerrorPi :: Double -> Int\nerrorPi x = head [n | n <- [1..]\n                    , abs (pi - aproximacionPi n) < x]\n\n-- 2\u00aa definici\u00f3n de errorPi\nerrorPi2 :: Double -> Int\nerrorPi2 x =\n  length (takeWhile (>=x) [abs (pi - 2 * fromRational y)\n                          | y <- productosWallis])\n\n-- 2\u00aa definici\u00f3n de aproximacionPi\naproximacionPi2 :: Int -> Double\naproximacionPi2 n =\n  2 * productosWallis2 !! n\n\nproductosWallis2 :: [Double]\nproductosWallis2 = scanl1 (*) factoresWallis2\n\nfactoresWallis2 :: [Double]\nfactoresWallis2 =\n  concat [[y\/(y-1),  y\/(y+1)] | x <- [1..], let y = 2*x]\n\n-- 3\u00aa definici\u00f3n de errorPi\nerrorPi3 :: Double -> Int\nerrorPi3 x = head [n | n <- [1..]\n                     , abs (pi - aproximacionPi2 n) < x]\n\n-- Comparaci\u00f3n de eficiencia\n--    \u03bb> errorPi 0.001\n--    1569\n--    (0.82 secs, 374,495,816 bytes)\n--\n--    \u03bb> errorPi2 0.001\n--    1569\n--    (0.79 secs, 369,282,320 bytes)\n--\n--    \u03bb> errorPi3 0.001\n--    1569\n--    (0.04 secs, 0 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>El producto de Wallis es una expresi\u00f3n, descubierta por John Wallis en 1655, para representar el valor de \u03c0 y que establece que: \u03c0 2 2 4 4 6 6 8 8 &#8212; = &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7 &#8212; \u00b7\u00b7\u00b7 2 1 3 3 5&#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":[130,8,12,365,71,28,11,366,252,34],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2921"}],"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=2921"}],"version-history":[{"count":9,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2921\/revisions"}],"predecessor-version":[{"id":2978,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2921\/revisions\/2978"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}