Click here to Skip to main content
16,020,111 members
Articles / Programming Languages / Python

Calculating the Number PI Through Infinite Sequences

Rate me:
Please Sign up or sign in to vote.
4.71/5 (23 votes)
2 Sep 2014CPOL4 min read 146.2K   198   23   32
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)


Written By
Software Developer
Brazil Brazil
I am a software developer focused on Mathematics, IoT and Games.
Homepage: HTML Apps
Blog: www.josecintra.com/blog

Comments and Discussions

 
QuestionNot impressed Pin
Jose A Pascoa8-Aug-17 6:48
Jose A Pascoa8-Aug-17 6:48 
Using the MPFR library I get PI with 1000 correct decimals in milliseconds and with 10000 correct decimals in under 2 seconds.
PI with 1000 decimals: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
AnswerRe: Not impressed Pin
José Cintra18-Aug-17 14:03
José Cintra18-Aug-17 14:03 
QuestionOn Today's Hardware Pin
Rick York21-Apr-17 8:03
mveRick York21-Apr-17 8:03 
AnswerRe: On Today's Hardware Pin
José Cintra18-Aug-17 14:06
José Cintra18-Aug-17 14:06 
QuestionNot just irrational.... Pin
ozbear14-Sep-14 2:42
ozbear14-Sep-14 2:42 
AnswerRe: Not just irrational.... Pin
José Cintra15-Sep-14 2:07
José Cintra15-Sep-14 2:07 
NewsAccurate Ratios for Pi. Pin
Osmund Francis9-Sep-14 1:35
professionalOsmund Francis9-Sep-14 1:35 
GeneralRe: Accurate Ratios for Pi. Pin
José Cintra9-Sep-14 5:55
José Cintra9-Sep-14 5:55 
GeneralRe: Accurate Ratios for Pi. Pin
Osmund Francis7-Oct-14 5:59
professionalOsmund Francis7-Oct-14 5:59 
QuestionSome nostalgia! Pin
Member 77034316-Sep-14 13:06
Member 77034316-Sep-14 13:06 
AnswerRe: Some nostalgia! Pin
José Cintra7-Sep-14 4:03
José Cintra7-Sep-14 4:03 
GeneralGood article! Pin
Jose David Pujo5-Sep-14 0:23
Jose David Pujo5-Sep-14 0:23 
GeneralRe: Good article! Pin
José Cintra5-Sep-14 4:29
José Cintra5-Sep-14 4:29 
GeneralMy vote of 3 Pin
Juan Manuel Romero Martin4-Sep-14 8:33
Juan Manuel Romero Martin4-Sep-14 8:33 
GeneralRe: My vote of 3 Pin
José Cintra5-Sep-14 6:16
José Cintra5-Sep-14 6:16 
QuestionIndiana Bill for Pi Pin
Matthew Rhea4-Sep-14 7:12
Matthew Rhea4-Sep-14 7:12 
AnswerRe: Indiana Bill for Pi Pin
José Cintra4-Sep-14 7:51
José Cintra4-Sep-14 7:51 
QuestionViete best? Pin
Kenneth Van Eetvelde4-Sep-14 3:28
Kenneth Van Eetvelde4-Sep-14 3:28 
AnswerRe: Viete best? Pin
José Cintra4-Sep-14 4:23
José Cintra4-Sep-14 4:23 
GeneralRe: Viete best? Pin
Mike Riley - QUSA4-Sep-14 11:31
Mike Riley - QUSA4-Sep-14 11:31 
GeneralRe: Viete best? Pin
José Cintra5-Sep-14 2:14
José Cintra5-Sep-14 2:14 
QuestionInteresting article Pin
Mike Hankey3-Sep-14 18:16
mveMike Hankey3-Sep-14 18:16 
AnswerRe: Interesting article Pin
José Cintra5-Sep-14 4:22
José Cintra5-Sep-14 4:22 
QuestionUsefulness of calclating PI Pin
swshurts3-Sep-14 3:40
swshurts3-Sep-14 3:40 
AnswerRe: Usefulness of calclating PI Pin
José Cintra3-Sep-14 4:00
José Cintra3-Sep-14 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.