i need some help on my pi approximation it's really slow. compile it and see for your self. i think my problem is with my math. it comes out with the wrong first few numbers. i'm trying to use gregory's formula or the advanced version of it.

#include <iostream>
#include <conio.h>
using namespace std;

void main()
{
 long double ans=4,l=1;
 cout<<ans<<endl;
 for(int i=1;i<=100000000;i++)
 {
 l++;
  if(i % 2 == 1)
  {
   ans=(ans-1/(2*l +1));
  }
  if(i % 2 == 0)
   {
   ans=(ans+1/(2*l+1));
   }
  system("cls");
  cout<< ans <<endl;
 }
 _getch();
}

> system("cls");
Have you any idea how long this takes?!!
This is a mountain, the rest of your program is a grain of sand by comparison.

You might also try

if ( i % 1000 == 0 ) {
    cout<< ans <<endl;
}

So you only print out every 1000th result (you're going to 100M remember).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.