#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
float a;
double pi;
double s;
double r;
double k;
int iterations;
int repetitions = 0;
cout << "How many iterations of the equation would you like? \n";
cout << "The first iteration yields 3.14.\nEach iteration triples the number of correct digits.";
cin >> iterations;
cout << "\nCalculating Pi...\n";
s = (sqrt(3)-1)/2;
a = 1.0/3;
while (iterations >= repetitions)
{
r = 3/(1+2*pow((1-pow(s,3.0)),(1/3)));
s = (r-1)/2;
a = pow(r,2)*a-pow(3,k)*(pow(r,2)-1);
repetitions++;
};
pi = 1/a;
cout << "Pi = " << pi;
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
float a;
double pi;
double s;
double r;
double k;
int iterations;
int repetitions = 0;
cout << "How many iterations of the equation would you like? \n";
cout << "The first iteration yields 3.14.\nEach iteration triples the number of correct digits.";
cin >> iterations;
cout << "\nCalculating Pi...\n";
s = (sqrt(3)-1)/2.0;
a = 1.0/3.0;
while (iterations >= repetitions)
{
r = 3/(1+2*pow((1-pow(s,3.0)),(1.0/(double)3)));
s = (r-1)/2.0;
a = pow(r,2)*a-pow(3,k)*(pow(r,2)-1);
repetitions++;
};
pi = 1/(double)a;
cout << "Pi = " << pi;
return 0;
}
The program requires only a small modification. Check the explicit conversions to double.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double a, pi, s, r, k;
int iter;
int rep = 0;
cout << "Iterations for PI accuracy" << endl;
cin >> iter;
s = (sqrt(3.0)- 1.0) / 2.0;
a = 1.0 / 3.0;
while(iter >= rep)
{
r = 3.0 / (1.0 + 2.0 * pow((1-pow(s, 3.0)), (1/3)));
s = (r - 1.0) / 2.0;
a = pow(r,2.0) * a - pow(3.0,k) * (pow(r,2.0) - 1);
rep++;
}
pi = 1.0 / a;
cout << "PI: " << pi << endl;
system("PAUSE");
return 0;
}
I changed it slightly, however you are trying to use variable 'k' without initializing it which
means that the code wont run no matter what you do. 'k' needs a value to work.
I was guessing that the value was 'n' or reps, however I added 'cout << rep << endl;' on the last line
of the while loop and noted that the loop was running over.
I am tired too... no sleep for like 2 days, but I can't help because I have no idea how
to use the formula you are trying to do.