diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-11-12 09:23:19 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-11-12 09:23:19 +0200 |
| commit | dbaf69f0e0ede46cbf5765cb6ca9500fcd7a3ea7 (patch) | |
| tree | 42890f66de25479419a2be48af695f79be7c155d /C++/LeibnizPI.cpp | |
| parent | da0cd9df96b09425416869e819af9633866374ab (diff) | |
| download | algorithms-dbaf69f0e0ede46cbf5765cb6ca9500fcd7a3ea7.tar algorithms-dbaf69f0e0ede46cbf5765cb6ca9500fcd7a3ea7.tar.gz algorithms-dbaf69f0e0ede46cbf5765cb6ca9500fcd7a3ea7.zip | |
Divided C and C++ files into seperate folders
Diffstat (limited to 'C++/LeibnizPI.cpp')
| -rw-r--r-- | C++/LeibnizPI.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/C++/LeibnizPI.cpp b/C++/LeibnizPI.cpp new file mode 100644 index 0000000..2243f61 --- /dev/null +++ b/C++/LeibnizPI.cpp @@ -0,0 +1,26 @@ +#include <iostream> + +using namespace std; + +int main() +{ + cout << "n = "; + unsigned int n = 1; + cin >> n; + long double pid4 = 1; + + for(unsigned int i = 0, dvsr = 3; i < n; i++, dvsr += 2) { + pid4 += ((i % 2) ? 1.0 : -1.0) / dvsr; + } + + cout.precision(64); + cout << endl << pid4 * 4.0 << endl; + + return 0; +} + +/* This is a quick implementation of of the Leibniz formula for PI + * https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 + * + * Fun fact: you need around 100 million iterations for 7 correct decimal places + */ |
