Skip to content

Instantly share code, notes, and snippets.

@JJ
Last active March 22, 2021 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JJ/eb09eefe5f2bd8ae7d0ea332378a51b9 to your computer and use it in GitHub Desktop.
Save JJ/eb09eefe5f2bd8ae7d0ea332378a51b9 to your computer and use it in GitHub Desktop.
Cool Perl 6 one liners vol. 2: computing Pi in a series
#You can also copy/paste this directly into the command line if perl6 is installed
raku -e "say π - 4 * ([+] <1 -1> <</<< (1,3,5,7,9...10000)) "
# This is an implementation of the Madhava-Lebniz series: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
# And a better, or at least shorter, version of https://gist.github.com/JJ/23801c82b218b25da7fc8831b7c331ad
# Instead of creating two series and combining them, this combines 1,-1 and the series of odd numbers
# by applying them successively: 1 divided by the first, -1 by the second, and so on. This creates exactly the same effect.
# It is faster and you only need to change one number, the 10000, to compute Pi with a bigger precision.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment