Skip to content

Instantly share code, notes, and snippets.

@JohnB
Last active January 6, 2025 05:56
Show Gist options
  • Save JohnB/19bef1ad52e705b1539a78d16ed71f1e to your computer and use it in GitHub Desktop.
Save JohnB/19bef1ad52e705b1539a78d16ed71f1e to your computer and use it in GitHub Desktop.
defmodule Leibniz do
# Finished - counter has reached the requested number of iterations
def pi(iterations, iterations, _numerator, result), do: result
def pi(iterations, counter, numerator, result) do
pi(iterations, counter + 1, -numerator, result + (numerator / (3 + 2 * counter)))
end
# Entry point
def pi(iterations), do: 4 * pi(iterations, 0, -1.0, 1.0)
end
Leibniz.pi(1000000)
# 3.141592663589326
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment