Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

#1 2018-06-09 18:13:36

Amartyanil
Member
From: Universe
Registered: 2013-05-27
Posts: 77

Calculating Pi

Hi;

I have written a code in C++ for calculating the approximate value of

based on the formula:

#include <iostream>

double CalculatePi(int acc)
{
int sone;
int stwo;
double pione = 0;
double pitwo = 0;
for (sone = 1; sone < acc; sone += 4) {
pione = pione + 1/sone;
}
for (stwo = 3; stwo < acc; sone += 4) {
pitwo = pitwo - 1/stwo;
}
double potentialpi;
potentialpi = pione + pitwo;
std::cout << potentialpi * 4 << std::endl;
return 0;
}

int main()
{
std::cout << "Enter accuracy: ";
int accuracy;
std::cin >> accuracy;
CalculatePi(accuracy);
return 0;
}

However, this code is not working properly and is not returning anything when I am inputting "acc"(for accuracy) more than 2.

Last edited by Amartyanil (2018-06-09 18:15:01)

"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon

Offline

#2 2018-06-09 19:34:00

bob bundy
Registered: 2010-06-20
Posts: 8,462

Re: Calculating Pi

hi Amartyanil

What exactly do you mean by "it's not working properly" ?  This series converges only very slowly.  I needed over 1200 terms before I got 3.14....

Bob

Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob Bundy

Offline

#3 2018-06-10 14:56:14

Amartyanil
Member
From: Universe
Registered: 2013-05-27
Posts: 77

Re: Calculating Pi

hi bob bundy;

For "not working properly", I mean that the program is getting frozen and not printing anything at all.
Can you please point out my code for bugs?

"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon

Offline

#4 2018-06-17 06:49:47

benice
Member
Registered: 2010-06-10
Posts: 117
Website

Re: Calculating Pi

pione = pione + 1/double(sone);

pitwo = pitwo - 1/double(stwo);

Offline

#5 2018-06-20 05:23:55

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,859
Website

Re: Calculating Pi

In line 12, you are incrementing sone instead of stwo. Is this intentional?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#6 2018-07-17 15:46:16

Amartyanil
Member
From: Universe
Registered: 2013-05-27
Posts: 77

Re: Calculating Pi

hi Benice;

Thanks for pointing out that the sone and stwo's would have been floats.

hi Agnishom;

That was not intentional. Thanks for pointing out.

However, can anyone please explain why the code was not working properly when I entered the datatype of sone and stwo as "int"?

Last edited by Amartyanil (2018-07-17 15:52:09)

"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon

Offline

#7 2018-07-24 01:09:45

George,Y
Member
Registered: 2006-03-12
Posts: 1,333

Re: Calculating Pi

I suggest you test if the problem is a pure IO one.

Just change the code to compulsively calculate the function on a certain accuracy you put in the code.

And compile it to see if it runs properly.

Then alter the code with another value of accuracy, and test again.

If both of them succeeded,  the mistake is located in main

X'(y-Xβ)=0

Offline

#8 2018-07-26 01:25:15

Amartyanil
Member
From: Universe
Registered: 2013-05-27
Posts: 77

Re: Calculating Pi

George,Y wrote:

If both of them succeeded,  the mistake is located in main

"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon

Offline

#9 2018-07-29 14:43:59

Amartyanil
Member
From: Universe
Registered: 2013-05-27
Posts: 77

Re: Calculating Pi

hi;

Will using this formula in the code have a lesser complexity than the code originally posted?

\pi = 4 \sum_{k \geq 0} (-1)^k {1\over 2k + 1}

Last edited by Amartyanil (2018-07-29 14:47:36)

"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon

Offline

#10 2018-08-04 15:25:08

George,Y
Member
Registered: 2006-03-12
Posts: 1,333

Re: Calculating Pi

Amartyanil wrote:
George,Y wrote:

If both of them succeeded,  the mistake is located in main

The program now needs an input "accuracy" to start running.

I suggest to cancel this input first and set an accuracy inside the code.

Compile and test it.

X'(y-Xβ)=0

Offline