This comes from http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/

"Given that Pi can be estimated using the function \$4 \times (1 - 1/3 + 1/5 - 1/7 + \cdots)\$ 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.
  • 10
    \$\begingroup\$ You should probably add some more rules, otherwise you will get answers like (python) p=lambda:3.14159 \$\endgroup\$
    – Matt
    Commented Aug 29, 2012 at 15:44
  • 1
    \$\begingroup\$ Have you seen codegolf.stackexchange.com/questions/506/… , which is very similar? At the very least, trig functions should be banned for this problem because they allow for trivial solutions such as this QBASIC program: ?INT(4E5*ATN(1))/1E5 \$\endgroup\$ Commented Aug 29, 2012 at 17:30
  • \$\begingroup\$ I think you should require that the algorithm be one of successive approximation: the longer you compute, the closer you get to pi. \$\endgroup\$
    – DavidC
    Commented Aug 29, 2012 at 22:59
  • 2
    \$\begingroup\$ Dupe, but it's so old it's not here: stackoverflow.com/q/407518/12274 \$\endgroup\$
    – J B
    Commented Sep 1, 2012 at 21:42
  • 4
    \$\begingroup\$ I’m voting to close this question because use of a specific algorithm is impossible to objectively validate \$\endgroup\$
    – ATaco
    Commented Jul 20, 2023 at 3:51

31 Answers 31


CJam, 20 bytes


Try it online!

One byte shorter than aditsu's answer. ;)

Alternate 20-byter


Try it online!

Does the same thing, but I like this better for some reason.


Not the answer you're looking for? Browse other questions tagged or ask your own question.