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: 67

### 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,371

### 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

Offline

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

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

### 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: 115
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,856
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