Skip to content

Instantly share code, notes, and snippets.

@pr4v33n
Created March 20, 2011 20:13
Show Gist options
  • Save pr4v33n/878634 to your computer and use it in GitHub Desktop.
Save pr4v33n/878634 to your computer and use it in GitHub Desktop.
Pi Approximation
// http://min.us/mvk51zd
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
double machin(void) {
long double pi_approx = 0.0L, t;
for(int k=0; k<10; ++k) {
t = 1.0L + 2.0L*k;
pi_approx += -(pow(-1.0L,k)*pow(1195.0L,-t)*(pow(5.0L,t)-4.0*pow(239.0L,t)))/t;
cout << setprecision(12) << "Iteration " << k << ": " << 4.0L*pi_approx << endl;
}
return 4.0*pi_approx;
}
int main(int argc, char *argv[]) {
cout << "Final Approximation: " << setprecision(12) << machin() << endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment