15,922,650 members
Articles / Programming Languages / Python

# Calculating the Number PI Through Infinite Sequences

Rate me:
2 Sep 2014CPOL4 min read 142.5K   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:

## 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);

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);

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);

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);

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: ")

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.

See you soon..

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

 First PrevNext
 Not impressed Jose A Pascoa8-Aug-17 6:48 Jose A Pascoa 8-Aug-17 6:48
 Re: Not impressed José Cintra18-Aug-17 14:03 José Cintra 18-Aug-17 14:03
 On Today's Hardware Rick York21-Apr-17 8:03 Rick York 21-Apr-17 8:03
 Re: On Today's Hardware José Cintra18-Aug-17 14:06 José Cintra 18-Aug-17 14:06
 Not just irrational.... ozbear14-Sep-14 2:42 ozbear 14-Sep-14 2:42
 Re: Not just irrational.... José Cintra15-Sep-14 2:07 José Cintra 15-Sep-14 2:07
 Accurate Ratios for Pi. Osmund Francis9-Sep-14 1:35 Osmund Francis 9-Sep-14 1:35
 Re: Accurate Ratios for Pi. José Cintra9-Sep-14 5:55 José Cintra 9-Sep-14 5:55
 Re: Accurate Ratios for Pi. Osmund Francis7-Oct-14 5:59 Osmund Francis 7-Oct-14 5:59
 Some nostalgia! Member 77034316-Sep-14 13:06 Member 7703431 6-Sep-14 13:06
 Re: Some nostalgia! José Cintra7-Sep-14 4:03 José Cintra 7-Sep-14 4:03
 Good article! Jose David Pujo5-Sep-14 0:23 Jose David Pujo 5-Sep-14 0:23
 Re: Good article! José Cintra5-Sep-14 4:29 José Cintra 5-Sep-14 4:29
 My vote of 3 Juan Manuel Romero Martin4-Sep-14 8:33 Juan Manuel Romero Martin 4-Sep-14 8:33
 Re: My vote of 3 José Cintra5-Sep-14 6:16 José Cintra 5-Sep-14 6:16
 Indiana Bill for Pi Matthew Rhea4-Sep-14 7:12 Matthew Rhea 4-Sep-14 7:12
 Re: Indiana Bill for Pi José Cintra4-Sep-14 7:51 José Cintra 4-Sep-14 7:51
 Viete best? Kenneth Van Eetvelde4-Sep-14 3:28 Kenneth Van Eetvelde 4-Sep-14 3:28
 Re: Viete best? José Cintra4-Sep-14 4:23 José Cintra 4-Sep-14 4:23
 Re: Viete best? Mike Riley - QUSA4-Sep-14 11:31 Mike Riley - QUSA 4-Sep-14 11:31
 Re: Viete best? José Cintra5-Sep-14 2:14 José Cintra 5-Sep-14 2:14
 Interesting article Mike Hankey3-Sep-14 18:16 Mike Hankey 3-Sep-14 18:16
 Re: Interesting article José Cintra5-Sep-14 4:22 José Cintra 5-Sep-14 4:22
 Usefulness of calclating PI swshurts3-Sep-14 3:40 swshurts 3-Sep-14 3:40
 While I appreciate the elegance of your solution and the intellectual curiosity of such an endeavor, given that PI to the 57th decimal place can ascribe a circle around the entire known universe with an inaccuracy of less than a millionth of an inch, what practical purpose is served by calculating PI to a 1000 or more decimal places? I believe that going from 999 to 1000 places took the computer (I'm sure it was a background process) more than 3 years to calculate. Seems like that computing time could have been spent doing cancer or Alzheimer's research. Steve Shurts [DELETED] 207 Haskins Road Bowling Green, OH 43402-2206 (419) [DELETED]modified 11-Dec-20 8:40am.
 Re: Usefulness of calclating PI José Cintra3-Sep-14 4:00 José Cintra 3-Sep-14 4:00
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jun-24 18:27 Refresh 12 Next ᐅ