check this c++ code and please correct

Cubic conversion of Borwein's Algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
 
using namespace 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;
}




Check this.
maybe you can understand this ....

Borwein's algorithm (others) - 2. Cubical convergence, 1991:
http://en.wikipedia.org/wiki/Borwein%27s_algorithm_%28others%29

it is quite difficult to solve this problem for me because i am a beginner.




Check my mistakes and correct please.
i am tired.

bye
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
 
using namespace 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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>

using namespace 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.

Check this.
maybe you can understand this ....

Borwein's algorithm (others) - 2. Cubical convergence, 1991:
http://en.wikipedia.org/wiki/Borwein%27s_algorithm_%28others%29

it is quite difficult to solve this problem for me because i am a beginner.
Last edited on
Topic archived. No new replies allowed.