Click here to Skip to main content
13,189,884 members (50,295 online)
Click here to Skip to main content
Add your own
alternative version

Stats

43.9K views
123 downloads
20 bookmarked
Posted 2 Sep 2014

Calculating the Number PI Through Infinite Sequences

, 2 Sep 2014
Rate this:
Please Sign up or sign in to vote.
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:

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

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:

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


 

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)
   
Leibniz90000000024.48
Nilakantha - Double Precision5003.16
Viete152.2
Wallis90000000014.4
Nilakantha - Arbitrary Precision3503.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)

Share

About the Author

José Cintra
Software Developer
Brazil Brazil
José Augusto Cintra is a IT analyst, teacher and game developer.
Homepage: HTML Apps
Blog: www.josecintra.com/blog

You may also be interested in...

Comments and Discussions

 
QuestionNot impressed Pin
Jose A Pascoa8-Aug-17 6:48
memberJose A Pascoa8-Aug-17 6:48 
AnswerRe: Not impressed Pin
José Cintra18-Aug-17 14:03
memberJosé Cintra18-Aug-17 14:03 
QuestionOn Today's Hardware Pin
Rick York21-Apr-17 8:03
memberRick York21-Apr-17 8:03 
AnswerRe: On Today's Hardware Pin
José Cintra18-Aug-17 14:06
memberJosé Cintra18-Aug-17 14:06 
QuestionNot just irrational.... Pin
ozbear14-Sep-14 2:42
memberozbear14-Sep-14 2:42 
AnswerRe: Not just irrational.... Pin
Jose Cintra15-Sep-14 2:07
memberJose Cintra15-Sep-14 2:07 
NewsAccurate Ratios for Pi. Pin
Osmund Francis9-Sep-14 1:35
memberOsmund Francis9-Sep-14 1:35 
GeneralRe: Accurate Ratios for Pi. Pin
Jose Cintra9-Sep-14 5:55
memberJose Cintra9-Sep-14 5:55 
GeneralRe: Accurate Ratios for Pi. Pin
Osmund Francis7-Oct-14 5:59
memberOsmund Francis7-Oct-14 5:59 
QuestionSome nostalgia! Pin
Member 77034316-Sep-14 13:06
memberMember 77034316-Sep-14 13:06 
AnswerRe: Some nostalgia! Pin
Jose Cintra7-Sep-14 4:03
memberJose Cintra7-Sep-14 4:03 
GeneralGood article! Pin
Jose David Pujo5-Sep-14 0:23
memberJose David Pujo5-Sep-14 0:23 
GeneralRe: Good article! Pin
Jose Cintra5-Sep-14 4:29
memberJose Cintra5-Sep-14 4:29 
GeneralMy vote of 3 Pin
Juan Manuel Romero Martin4-Sep-14 8:33
memberJuan Manuel Romero Martin4-Sep-14 8:33 
GeneralRe: My vote of 3 Pin
Jose Cintra5-Sep-14 6:16
memberJose Cintra5-Sep-14 6:16 
QuestionIndiana Bill for Pi Pin
Matthew Rhea4-Sep-14 7:12
memberMatthew Rhea4-Sep-14 7:12 
AnswerRe: Indiana Bill for Pi Pin
Jose Cintra4-Sep-14 7:51
memberJose Cintra4-Sep-14 7:51 
QuestionViete best? Pin
Kenneth Van Eetvelde4-Sep-14 3:28
memberKenneth Van Eetvelde4-Sep-14 3:28 
AnswerRe: Viete best? Pin
Jose Cintra4-Sep-14 4:23
memberJose Cintra4-Sep-14 4:23 
GeneralRe: Viete best? Pin
Mike Riley - QUSA4-Sep-14 11:31
memberMike Riley - QUSA4-Sep-14 11:31 
GeneralRe: Viete best? Pin
Jose Cintra5-Sep-14 2:14
memberJose Cintra5-Sep-14 2:14 
QuestionInteresting article Pin
Mike Hankey3-Sep-14 18:16
professionalMike Hankey3-Sep-14 18:16 
AnswerRe: Interesting article Pin
Jose Cintra5-Sep-14 4:22
memberJose Cintra5-Sep-14 4:22 
QuestionUsefulness of calclating PI Pin
swshurts3-Sep-14 3:40
memberswshurts3-Sep-14 3:40 
AnswerRe: Usefulness of calclating PI Pin
Jose Cintra3-Sep-14 4:00
memberJose Cintra3-Sep-14 4:00 
GeneralRe: Usefulness of calclating PI Pin
Amarnath S3-Sep-14 18:38
memberAmarnath S3-Sep-14 18:38 
GeneralRe: Usefulness of calclating PI Pin
Chad3F9-Sep-14 11:09
memberChad3F9-Sep-14 11:09 
GeneralRe: Usefulness of calclating PI Pin
Jose Cintra10-Sep-14 8:54
memberJose Cintra10-Sep-14 8:54 
QuestionMy vote of 4 Pin
Kenneth Haugland2-Sep-14 21:32
professionalKenneth Haugland2-Sep-14 21:32 
AnswerRe: My vote of 4 Pin
Jose Cintra3-Sep-14 2:32
memberJose Cintra3-Sep-14 2:32 
GeneralRe: My vote of 4 Pin
Kenneth Haugland3-Sep-14 5:42
professionalKenneth Haugland3-Sep-14 5:42 
GeneralRe: My vote of 4 Pin
Jose Cintra3-Sep-14 6:13
memberJose Cintra3-Sep-14 6:13 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 3 Sep 2014
Article Copyright 2014 by José Cintra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid