PI Code

I need help with an assignment, I don't know how to go about this in order to calculate my data.
I have to calculate 3 different formulas in order to find the value of pi.

Euler for all integers
Which is pi^2/6=1/1^2+1/2^2+1/3^2 ... etc

Using loops
When I execute it will use numbers from a file to see how many times it will run the code. Example: 1 run will equal to 2.449489742783 and 6 runs equal. 2.991376494748

How do I make it so my code will
Here's my code.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;


int main()
{
  int data;
  double euall, euall2, euall3, euall4;
  double eual, eual2, eual3, eual4;
  double eulerall;
  int eunum, eunum2;
  int eunm, eunm2;
  int count, count2, count3;
  int i, j;
  int half;

  cout << setprecision(13);
  count = 0;
  count3 = 0;
  eunum = 0;
  eunm = 0;
  cin >> data;


    while(cin)
    {
      half=data/2;
      for (i=0; i<data; i++)
        {
      eunum = eunum++;
      euall2 = (pow(eunum,2));
      //      cout << euall2 << endl;                                                                                                                                                                            
      euall3 = 1/euall2;
      //      cout << euall3 << endl;                                                                                                                                                                            
      euall4 = 6 * euall3;
      //      cout << euall4 << endl;                                                                                                                                                                            
      euall = sqrt(euall4);
      //      cout << euall << endl;                                                                                                                                                                             

            if(half > 1)
        {
      eunm = eunm + 2;
      eual2 = (pow(eunm,2));
      //      cout << euall2 << endl;                                                                                                                                                                            
      eual3 = 1/eual2;
      //      cout << euall3 << endl;                                                                                                                                                                            
      eual4 = 6 * eual3;
      //      cout << euall4 << endl;                                                                                                                                                                            
      eual = sqrt(eual4);
      eulerall = eual + euall;
      //      cout << eual << endl;                                                                                                                                                                              
      //      cout << euall << endl;                                                                                                                                                                             
      cout << "Greater"  << endl;
        }
        }
            cin >> data;
    }

    cout << euall << endl;

  return 0;
}
Last edited on
Isn't the formula to calculate pi:
4/1 - 4/3 + 4/5 - 4/7..
4 + 4/(1+2n)(-1)^n...

so something like:

1
2
3
4
5
6
7
double pi = 4.0;
int sign = -1;
for(int i = 1; i <= n; ++i)
{
    pi += 4.0/( 1 + 2.0*i )*sign;
    sign = -sign;
}


http://coliru.stacked-crooked.com/a/bbf92e2241eb274c

You will need a bunch of iterations before getting a pretty accurate pi though.
Yeah the assignment required 3 calculation of pi,
That is one of them.
http://i61.tinypic.com/2j0knj4_th.png

The one you are talking about is this one.
http://i58.tinypic.com/vsjaed_th.png
well if ∏2/6 = 1/n2 + 1/(n+1)2 + ... then
2 = 6 * ( 1/n2+1/(n+1)2) + ... then
∏ = √(6*(1/n2+1/(n+1)2) + ...)

so something like:

1
2
3
4
5
6
7
8
9
10
double pi = 0.0;

for(int i = 0; i < n; ++i)
{
    pi += 1.0/(i*i);
}

pi *= 6;

pi = std::sqrt(pi);


http://coliru.stacked-crooked.com/a/cac0565559051997

I'm not even sure what you were trying to do with your code it is confusing :P
Last edited on
That should help, Ill post again if i have any more problems. Thank you so much!
I'm getting negative values for some reason with the output can you help me?
Can I see the code you used?
Topic archived. No new replies allowed.