Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Python

Calculating the Number PI Through Infinite Sequences

4.71/5 (23 votes)
2 Sep 2014CPOL4 min read 146.4K   198  
Iterative algorithms for computing approximations to the number PI through infinite series using double and arbitrary precision

"The circumference of any circle is greater than three times its diameter, and  the  excess is less than one seventh of the diameter but larger than ten times its Seventy first part " - Archimedes 
 

Introduction

Introducing the number PI with their first 50 decimal places: 

3.1415926535897932384626433832795028841971693993751 

It is an irrational and transcendental number. Its decimal part is an infinite succession of numbers and their calculation became a classical problem of computational mathematics. This is because a lot of processing power is necessary for their generation and, therefore, more efficient algorithms.

Throughout history it proved possible to obtain the digits of PI with a certain "precision" through infinite series and is what we will do in this article.
We Warn, however, that the practical usefulness of the algorithms presented here is questionable because, in most situations, it is sufficient computing the PI with six decimal places, and therefore a much efficient algorithm for this would be as follows: 

function double PI(): { 

return (3.141593);

}

 
A bit of history

Traditionally, we define the PI as the ratio of the circumference and its diameter. Historically, however, was not always so. 

It is known that this irrational number arose on the calculations of geometers over time as a proportionality constant for at least 4 relationships, not necessarily in this order: 

  • Between the circumference of a circle to its diameter;
  • Between the area of a circle and the square of its diameter; 
  • Between the area of a sphere and the square of its diameter; 
  • Between the volume of a sphere and the cube of its diameter; 

The earliest known written references of the PI come from Babylon around 2000 BC. Since then, their approximations have gone through several transformations until they reach the billions of digits obtained today with the aid of the computer. 

Historically, one of the best approximations of PI and interestingly also one of the oldest, was used by the Chinese mathematician Zu Chongzhi (Sec.450 DC), which related the PI as "something" between 3.1415926 and 3.1415927. 

The calculation of PI has been revolutionized by the development of techniques of infinite series, especially by mathematicians from europe in the 16th and 17th centuries. 
An infinite series is the sum (or product) of the terms of an infinite sequence. That approach was first discovered in India sometime between 1400 and 1500 AD.

Now let's look at the main discoveries in this area:

Viete's Series
The first infinite sequence discovered in Europe was an infinite product, found by French mathematician François Viète in 1593:

Image 1

Wallis's Series
The second infinite sequence, found in Europe by John Wallis in 1655, was also an infinite product:

Image 2

Leibniz's Series
Madhava of Sangamagrama, a Indian mathematician, formulated a series that was rediscovered by scottish mathematician James Gregory in 1671, and by Leibniz in 1674:

Image 3

Nilakantha's Series
An infinite series for PI published by Nilakantha in the 15th century is:

Image 4
 

Background

To test the algorithms presented here, i suggest the following IDE: Orwell Dev-C++
 

Using the code

Before implementing the algorithms presented here in a production environment, it is necessary to validate the input data, since the primitive data types have a limited range of values that are hardware-dependent. The "double" type provides an accuracy of 16-20 digits.

The last algorithm uses data types with arbitrary precision (big numbers), so it is possible to obtain the PI number with a greater number of decimal places (100 digits, configurable).

Our purpose here, however, is more modest. We want to get the PI with 8 decimal places and then make a comparison between the methods.

Let's go to the algorithms!

1) Vietes's Series - Double Precision

// Approximation of the number PI through the Viete's series
// Language: C
// Author: Jose Cintra (jose.cintra@html-apps.info)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
   double n, i, j;     // Number of iterations and control variables
   double f;           // factor that repeats
   double pi = 1;
      
   printf("Approximation of the number PI through the Viete's series\n");
   printf("\nEnter the number of iterations: ");
   scanf("%lf",&n);
   printf("\nPlease wait. Running...\n");   
    
   for(i = n; i > 1; i--) {
      f = 2;
      for(j = 1; j < i; j++){
         f = 2 + sqrt(f);
      }
      f = sqrt(f);
      pi = pi * f / 2;
   }
   pi *= sqrt(2) / 2;
   pi = 2 / pi;
   
   printf("\nAproximated value of PI = %1.16lf\n", pi);  

}

2) Wallis's Series - Double Precision

// Approximation of the number pi through the Wallis's series
// Language: C
// Author: Jose Cintra (jose.cintra@html-apps.info)

#include <stdio.h>
#include <stdlib.h>

main() {

   double n, i         // Number of iterations and control variable
   double pi = 4;

   printf("Approximation of the number pi through the Wallis's series\n");
   printf("\nEnter the number of iterations: ");    
   scanf("%lf",&n);
   printf("\nPlease wait. Running...\n");      

   for(i = 3; i <= (n + 2); i+=2)    
      pi = pi * ((i - 1) / i) * (( i + 1) / i);

   printf("\nAproximated value of PI = %1.16lf\n", pi);  

}

3) Leibniz's Series - Double Precision

// Approximation of the number PI through the Leibniz's series
// Language: C
// Author: Jose Cintra (jose.cintra@html-apps.info)

#include <stdio.h>
#include <stdlib.h>

main() {

   double n, i;       // Number of iterations and control variable
   double s = 1;      //Signal for the next iteration
   double pi = 0;

   printf("Approximation of the number PI through the Leibniz's series\n");
   printf("\nEnter the number of iterations: ");    
   scanf("%lf",&n);
   printf("\nPlease wait. Running...\n");      

   for(i = 1; i <= (n * 2); i += 2){
     pi = pi + s * (4 / i);
     s = -s;
   }

   printf("\nAproximated value of PI = %1.16lf\n", pi);  

}

4) Nilakantha's Series - Double Precision

// Approximation of the number pi through the Nilakantha's series
// Language: C
// Author: Jose Cintra (jose.cintra@html-apps.info)

#include <stdio.h>
#include <stdlib.h>

main() {

   double n, i;    // Number of iterations and control variable
   double s = 1;   //Signal for the next operation
   double pi = 3;

   printf("Approximation of the number PI through the sequence of the Nilakantha's series\n");
   printf("\nEnter the number of iterations: ");    
   scanf("%lf",&n);
   printf("\nPlease wait. Running...\n");      

   for(i = 2; i <= n*2; i += 2){
     pi = pi + s * (4 / (i * (i + 1) * (i + 2)));
     s = -s;
   }

   printf("\nAproximated value of PI = %1.16lf\n", pi);  

}

5) Nilakantha's Series - Arbitrary Precision

# Approximation of the number PI through the Nilakantha's series
# Arbitrary precision
# Language: Python
# Author: Jose Cintra (jose.cintra@html-apps.info)

from decimal import *
getcontext().prec = 100

s = Decimal(1);   #Signal for the next operation
pi = Decimal(3);

print ("Approximation of the number PI through the Nilakantha's series\n")
n = input("Enter the number of iterations: ")    

print("\nPlease wait. Running...\n");      

for i in range (2, n * 2, 2):
    pi = pi + s * (Decimal(4) / (Decimal(i) * (Decimal(i) + Decimal(1)) * (Decimal(i) + Decimal(2))))
    s = -1 * s

print ("Aproximated value of PI :")
print (pi)

 

The Results

Below are the tests performed with each of the algorithms for calculating pi to 8 decimal places (3.14159265).

Compiler: MinGW - GCC 4.8.1 - 64 bit
Processor: I3 - 2.10GHz

  Iterations (n) Time (seconds)
     
Leibniz 900000000 24.48
Nilakantha - Double Precision 500 3.16
Viete 15 2.2
Wallis 900000000 14.4
Nilakantha - Arbitrary Precision 350 3.70

Obs: Test results are not conclusive because they were not performed with proper techniques. Furthermore, several factors can influence, such as the compiler, algorithm, computer, etc.

Conclusion

This is it!

I leave the conclusion to you when examining the table above. The real purpose was to have fun with these amazing formulas!

Hope this helps.  Questions and comments are welcome.

Download the source code here

See you soon..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)