/* Archimedes_Pi.cpp :

   This simple C++ program will calculate Pi to more decimal places than anyone ever really
  needs using the same method that Archimedes pioneered.  With only an abacus available for
  computing, he had to stop at 96 sides; still, that provided more than enough accuracy for
  even most of the projects of our own day (let alone way back then)!

   Note: This program was originally C source code, and some of it still is.
         See note further below as to why a C++ compiler was required.

   Our source code was based upon what Philip J. Erdelsky provided here:

   http://www.efgh.com/math/pi.htm 

     There's also a video showing step by step how to arrive at the same values and how to
   create an OpenOffice Spreadsheet for doing so at this URL:
   https://www.youtube.com/watch?v=_rJdkhlWZVQ

   (All additional C++ coding and comments are Copyrighted 2025 by Daniel B. Sedory)

*/

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

// Begin with a Hexagon inscribed in a Circle of Radius 1:
// So initialize the variable values n and s to: 6 and 1.

int main()
{
	double n = 6.0; // n = Sides; start with a Hexagon (6 sides).
	double s = 1.0; // A  hexagon  in a circle of diameter 2 
					// units has 6 sides of unit length of 1.
	double h;		// In "Visual C++ 2008" (or later) these two
	double x;		// declarations had to be made outside of
					// the "while()" function used below.

	cout << "\n    Archimedes' Method for Calculating Pi\n";
	cout << "(using the length of the sides of polygons):\n";
	cout << "\n  Number of Sides    Approximate Value of Pi\n";
	cout << "  ---------------    -----------------------\n";

	while (n < 200000000.0)
	{
		printf("  %9.0f          %18.16f\n", n, n * s / 2.0);
		h = s / 2.0;

		/*
			This program was first created under Linux, and the
			only reason it had to be compiled with g++ (the GNU
			C++ compiler) instead of gcc or some other C compiler
			was because C's math.h header does not include the
			Square Root function used below: sqrt().
		*/

		x = sqrt(1.0 - h * h);

/* Note: We could have used an intermediary variable here of y = 1.0 - x,
		 and then the following would be: s = sqrt(h*h + y*y);
		 but it seemed more efficient to reduce the number of variables.
*/
		s = sqrt(h * h + (1.0 - x) * (1.0 - x));
		n *= 2;
	}

	cout << "\n      Note:          3.1415926535897932 is\n";
	cout << "      the actual value of Pi to 16 places.\n\n";


// I'd recommend "commenting out" these two lines under linux, since
// they would never be necessary:

	cout << "\n Press ENTER to close window.\n\n";
	cin.ignore();	// This is merely a clever way (not how this function was intended
					// to be used) to pause the program until you press the ENTER key.
}

// Comments added by Visual Studio 2019:

// Run program: Ctrl + F5 or Debug > Start without Debugging menu
// Debug program: F5 or Debug > Start with Debugging menu

/* Tips for Getting Started:
   1. Use the Solution Explorer window to add/manage files
   2. Use the Team Explorer window to connect to source control
   3. Use the Output window to see build output and other messages
   4. Use the Error List window to view errors
*/

