Last active
January 6, 2025 05:56
-
-
Save JohnB/19bef1ad52e705b1539a78d16ed71f1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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