{"id":620,"date":"2010-09-13T15:48:29","date_gmt":"2010-09-13T15:48:29","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/programas-compactos-para-calcular-pi-con-la-formula-de-leibniz\/"},"modified":"2010-12-22T16:48:06","modified_gmt":"2010-12-22T16:48:06","slug":"programas-compactos-para-calcular-pi-con-la-formula-de-leibniz","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/programas-compactos-para-calcular-pi-con-la-formula-de-leibniz\/","title":{"rendered":"Programas compactos para calcular pi con la f\u00f3rmula de Leibniz"},"content":{"rendered":"<p>En art\u00edculos anteriores hemos comparado la eficiencia de programas en distintos lenguajes. En este vamos a comparar la simplicidad de los programas para resolver un problema.<\/p>\n<p>\nComo ejemplo he elegido el <i>problema del c\u00e1lculo compacto del n\u00famero <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Cpi&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;pi\" class=\"latex\" \/> mediante la f\u00f3rmula de Leibniz<\/i><br \/>\n<center><br \/>\n<img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%3Cbr+%2F%3E++++%5Cpi+%3D+4+%5Ctimes+%5Cleft%281+-+%5Cdfrac%7B1%7D%7B3%7D+%2B+%5Cdfrac%7B1%7D%7B5%7D+-+%5Cdfrac%7B1%7D%7B7%7D+%2B+%5Cdots%5Cright%29%3Cbr+%2F%3E+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&lt;br \/&gt;    &#92;pi = 4 &#92;times &#92;left(1 - &#92;dfrac{1}{3} + &#92;dfrac{1}{5} - &#92;dfrac{1}{7} + &#92;dots&#92;right)&lt;br \/&gt; \" class=\"latex\" \/><br \/>\n<\/center><br \/>\nEl enunciado de problema es el siguiente<\/p>\n<blockquote><p>\nEscribir un programa, con el menor n\u00famero posible de caracteres, para calcular el n\u00famero <img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%5Cpi&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&#92;pi\" class=\"latex\" \/> usando la f\u00f3rmula de Leibniz con un error menor que 0.00001.\n<\/p><\/blockquote>\n<p>\nEl problema se ha planteado en <a href=\"http:\/\/stackoverflow.com\/questions\/407518\/code-golf-leibniz-formula-for-pi\">Code Golf: Leibniz formula for Pi<\/a> y se han escrito distintas respuestas que resumo al final del art\u00edculo. Antes voy a presentar programas compactos en nuestros lenguajes habituales (Haskell, Maxima y Common Lisp).<\/p>\n<p><!--more--><\/p>\n<h2>Programa en Haskell (con 34 caracteres)<\/h2>\n<pre lang=\"haskell\">\r\n4*sum[(-1)**x\/(2*x+1)|x<-[0..1e5]]\r\n<\/pre>\n<p>Una sesi\u00f3n es <\/p>\n<pre lang=\"shell\">\r\nPrelude> 4*sum[(-1)**x\/(2*x+1)|x<-[0..1e5]]\r\n3.1416026534897203\r\nPrelude> abs(pi-4*sum[(-1)**x\/(2*x+1)|x<-[0..1e5]]) < 1e-5\r\nTrue\r\n<\/pre>\n<h2>Programa en Haskell (con 27 caracteres)<\/h2>\n<pre lang=\"haskell\">\r\nfoldr1(-)$map(4\/)[1,3..2e5]\r\n<\/pre>\n<p>Una sesi\u00f3n es<\/p>\n<pre lang=\"shell\">\r\n> foldr1(-)$map(4\/)[1,3..2e5]\r\n3.141602653489794\r\n> abs(pi-(foldr1(-)$map(4\/)[1,3..2e5])) < 1e-5\r\nTrue\r\n<\/pre>\n<h2>Programa en Maxima (con 38 caracteres)<\/h2>\n<pre lang=\"shell\">\r\nbfloat(4*sum((-1)**x\/(2*x+1),x,0,1e5));\r\n<\/pre>\n<p>Una sesi\u00f3n es<\/p>\n<pre lang=\"shell\">\r\n>bfloat(4*sum((-1)**x\/(2*x+1),x,0,1e5));\r\n3.141602653489794b0\r\n> is(abs(%pi-bfloat(4*sum((-1)**x\/(2*x+1),x,0,1e5)))<1e-5);\r\ntrue\r\n<\/pre>\n<h2>Programa en Common Lisp (con 55 caracteres)<\/h2>\n<pre lang=\"lisp\">\r\n(loop for i from 1 upto 3e5 by 4 sum (\/ 8d0 i (+ i 2)))\r\n<\/pre>\n<p>Una sesi\u00f3n es<\/p>\n<pre lang=\"shell\">\r\n> (loop for i from 1 upto 3e5 by 4 sum (\/ 8d0 i (+ i 2)))\r\n3.141585986923141d0\r\n> (< (abs (- pi (loop for i from 1 upto 3e5 by 4 sum (\/ 8d0 i (+ i 2))))) 1e-5)\r\nT\r\n<\/pre>\n<h2>Resumen de la compacidad de los programas<\/h2>\n<p>\nLa compacidad de los programas publicados en <a href=\"http:\/\/stackoverflow.com\/questions\/407518\/code-golf-leibniz-formula-for-pi\">Code Golf: Leibniz formula for Pi<\/a> se resume en la siguiente tabla<br \/>\n<img decoding=\"async\" src=\"https:\/\/s0.wp.com\/latex.php?latex=%3Cbr+%2F%3E+%5Cbegin%7Barray%7D%7B%7Cl%7Cr%7C%7D++%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BLenguaje%7D++++%26+%5Cmathrm%7BCaracteres%7D+%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BAWK%7D++++++++++%26+64+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BBrainfuck%7D++++%26+51+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BC%7D++++++++++++%26+67+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BC%5C%23%7D++++++++++%26+60+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BCommon%5C+Lisp%7D+%26+55+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7Bdc%7D+++++++++++%26+35+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BF%5C%23%7D++++++++++%26+59+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BFortran77%7D++++%26+248++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BHaskell%7D+++++++%26+27+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BJ%7D++++++++++++%26+14+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BJava%7D+++++++++%26+117++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BJavaScript%7D+++%26+43+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BLua%7D++++++++++%26+46+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BMatlab%7D+++++++%26+23+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BMathematica%7D++%26+27+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BOctave%7D+++++++%26+36+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BOracle%5C+SQL%7D++%26+73+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BPerl%7D+++++++++%26+42+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BPhyton%7D+++++++%26+51+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BRuby%7D+++++++++%26+33+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cmathrm%7BScheme%7D+++++++%26+95+++%5C%5C+%5Chline%3Cbr+%2F%3E+%5Cend%7Barray%7D%3Cbr+%2F%3E+&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002\" alt=\"&lt;br \/&gt; &#92;begin{array}{|l|r|}  &#92;hline&lt;br \/&gt; &#92;mathrm{Lenguaje}    &amp; &#92;mathrm{Caracteres} &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{AWK}          &amp; 64   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Brainfuck}    &amp; 51   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{C}            &amp; 67   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{C&#92;#}          &amp; 60   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Common&#92; Lisp} &amp; 55   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{dc}           &amp; 35   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{F&#92;#}          &amp; 59   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Fortran77}    &amp; 248  &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Haskell}       &amp; 27   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{J}            &amp; 14   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Java}         &amp; 117  &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{JavaScript}   &amp; 43   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Lua}          &amp; 46   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Matlab}       &amp; 23   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Mathematica}  &amp; 27   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Octave}       &amp; 36   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Oracle&#92; SQL}  &amp; 73   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Perl}         &amp; 42   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Phyton}       &amp; 51   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Ruby}         &amp; 33   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;mathrm{Scheme}       &amp; 95   &#92;&#92; &#92;hline&lt;br \/&gt; &#92;end{array}&lt;br \/&gt; \" class=\"latex\" \/><\/p>\n<h2>Conclusiones<\/h2>\n<p>De nuestros lenguajes habituales el programa m\u00e1s compacto es el de Haskell de 27 caracteres. <\/p>\n<p>\nDe todos los programas, el m\u00e1s corto es el siguiente programa en el <a href=\"http:\/\/en.wikipedia.org\/wiki\/J_(programming_language)\">lenguaje J <\/a> con s\u00f3lo 14 caracteres<\/p>\n<pre lang=\"J\">\r\n4*-\/%>:+:i.1e6\r\n<\/pre>\n<p>\nSi conoces programas m\u00e1s compactos para resolver el problema puedes escribirlo en los comentarios. Evidentemente, al no basarse en la f\u00f3rmula de Leibniz, no se admiten la siguiente respuesta (con 6 caracteres)<\/p>\n<pre lang=\"haskell\">\r\n3.1416\r\n<\/pre>\n<p>ni esta otra (con dos caracteres)<\/p>\n<pre lang=\"haskell\">\r\npi\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En art\u00edculos anteriores hemos comparado la eficiencia de programas en distintos lenguajes. En este vamos a comparar la simplicidad de los programas para resolver un problema. Como ejemplo he elegido el problema del c\u00e1lculo compacto del n\u00famero mediante la f\u00f3rmula de Leibniz El enunciado de problema es el siguiente Escribir un programa, con el menor&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","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":[5,92,74],"tags":[270,283,281,120,119],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/620"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/comments?post=620"}],"version-history":[{"count":12,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":1032,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/620\/revisions\/1032"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}