Calculate pi to 5 decimals This comes from http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/

"Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5 – 1/7 + …) with more terms giving greater accuracy, write a function that calculates Pi to an accuracy of 5 decimal places."


Note, the estimation must be done by calculating the sequence given above.
JavaScript, 46 58 56 45 bytes



ES6 update: Turns out there's more features available to us now that five years have passed.

let f=(i=0,a=0)=&gt;i&gt;1e6?a:f(i+4,a+8/-~i/(i+3))


This version (45 bytes; yes, the let is required) works in ES6 strict mode in theory.  In practice, you can run it in V8 (e.g. with node) with --use-strict --harmony-tailcalls; the Proper Tailcalls feature isn't widely implemented yet, alas.  However, it's specified behaviour, so it should be fine.

If we want to stick to what's widely implemented, and not require strict-mode, we can simply use the ES6 fat-arrow syntax for functions but otherwise retain the same implementation as before (suggested by Brian H) for a cost of 48 bytes.

a=&gt;{for(a=i=0;i&lt;1e6;a+=8/++i/~-(i+=3));return a}


The choice of name for the single parameter doesn't really matter, but we might as well pick one of the names we use so as to minimise the global-scope pollution.



function(){for(a=i=0;i&lt;1e6;a+=8/++i/~-(i+=3));return a}


This version is a function expression; add two characters (e.g. " f") if you want it named.  This version clobbers the globals a and i; this could be prevented if we added "a,i" to the parameter list.

Makes use of a reformulated version of the algorithm in order to circumvent the need for subtraction.

 1/1 - 1/3  +   1/5 - 1/7   +    1/9 - 1/11  + ...
(3/3 - 1/3) + (7/35 - 5/35) + (11/99 - 9/99) + ...
    2/3     +      2/35     +       2/99     + ...
  2/(1*3)   +    2/(5*7)    +     2/(9*11)   + ...


Here's a "plain" version without this adjustment:

function(){for(a=0,i=1;i&lt;1e6;i+=2)a+=[,4,,-4][i%4]/i;return a}


which clocks in at 64 62 characters.

Thanks to @ardnew for the suggestion to get rid of the 4* before the return.



History

function(){for(a=i=0;i&lt;1e6;a+=8/++i/~-(i+=3));return a}     // got rid of `i+=4`; restructured
// Old versions below.
function(){for(a=0,i=1;i&lt;1e6;i+=4)a+=8/i/-~-~i;return a}    // got rid of `4*`
function(){for(a=0,i=1;i&lt;1e6;i+=4)a+=2/i/-~-~i;return 4*a}