# Howto. Calculate PI.

I have the Applied Mathematics course in my university. The question: how to calculate the value of PI (3.14159265358979323846264338327950288...)? The answer: use Leibniz series: Small program for doing that:
ernestas@ernestas-laptop:~\$ cat calculate_pi.cpp #include <iostream> #include <math.h> using namespace std; double getPI(int k) { double pi = 0; for (int i = 0; i < k; i++) { pi += 4 * (pow(-1, i) / (2 * i + 1)); } return pi; } int main() { cout << "PI is: " << getPI(10) << endl; cout << "PI is: " << getPI(100) << endl; cout << "PI is: " << getPI(200) << endl; cout << "PI is: " << getPI(300) << endl; cout << "PI is: " << getPI(400) << endl; cout << "PI is: " << getPI(1000) << endl; cout << "PI is: " << getPI(2000) << endl; cout << "PI is: " << getPI(3000) << endl; cout << "PI is: " << getPI(4000) << endl; cout << "PI is: " << getPI(5000) << endl; cout << "PI is: " << getPI(7000) << endl; cout << "PI is: " << getPI(10000) << endl; return 0; } ernestas@ernestas-laptop:~\$ g++ calculate_pi.cpp -o pi && ./pi PI is: 3.04184 PI is: 3.13159 PI is: 3.13659 PI is: 3.13826 PI is: 3.13909 PI is: 3.14059 PI is: 3.14109 PI is: 3.14126 PI is: 3.14134 PI is: 3.14139 PI is: 3.14145 PI is: 3.14149 ernestas@ernestas-laptop:~\$
So, the bigger k is, the more accurate value we get :) P.S. If I'm not wrong, the value PI is defined in header <math.h> - but I'm not sure. :)
Žymos:

## Komentarai

### Sveiki, Geriausias būdas

Sveiki,

Geriausias būdas atrasti pi...
ta...da...:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609...