Calculate Pi using Nilkantha’s series
Last Updated :
29 Apr, 2022
Pi is an irrational number having non-recurring decimal values. We commonly know Pi = 3.14 or Pi = 22/7, but it is just an approximation for our ease. One way to calculate it can be given using Nilkantha’s series. It is given by –
Ï€ = 3 + 4 / (2*3*4) – 4 / (4*5*6) + 4 / (6*7*8) – . . .
Approach: On observing the pattern of the denominator it can be seen that for every term except the first one, it contains the multiplication of three consecutive numbers. We can use a variable and increment it by two on every iteration to get the correct term in the denominator. Further notice that this is alternating series i.e. sign of consecutive terms is different.
Follow the steps below to implement the above observations
- Create 3 variables n, Pi, sign
- Initialise Pi = 3, n = 2, sign = 1
- Iterate 0 to 1000000 to calculate for 1000000 terms and greater accuracy:
- At every iteration multiply sign = sign*(-1)
- Calculate Pi = Pi + sign*(4/(n) * (n+1) * (n+2))
- Increment n by 2 at every iteration
- Print the value of Pi
Below is the code to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double calculatePI( double PI, double n,
double sign)
{
for ( int i = 0; i <= 1000000; i++) {
PI = PI + (sign * (4 / ((n) * (n + 1)
* (n + 2))));
sign = sign * (-1);
n += 2;
}
return PI;
}
int main()
{
double PI = 3, n = 2, sign = 1;
cout << fixed << setprecision(8)
<< "The approximation of Pi is "
<< calculatePI(PI, n, sign) << endl;
return 0;
}
|
C
#include <stdio.h>
double calculatePI( double PI, double n,
double sign)
{
for ( int i = 0; i <= 1000000; i++) {
PI = PI + (sign * (4 / ((n) * (n + 1)
* (n + 2))));
sign = sign * (-1);
n += 2;
}
return PI;
}
void main()
{
double PI = 3, n = 2, sign = 1;
printf ( "The approximation of Pi is %0.8lf\n" ,calculatePI(PI, n, sign));
}
|
Java
import java.util.*;
class GFG {
static double calculatePI( double PI, double n,
double sign)
{
for ( int i = 0 ; i <= 1000000 ; i++) {
PI = PI + (sign * ( 4 / ((n) * (n + 1 )
* (n + 2 ))));
sign = sign * (- 1 );
n += 2 ;
}
return PI;
}
public static void main(String[] args)
{
double PI = 3 , n = 2 , sign = 1 ;
System.out.print( "The approximation of Pi is " );
System.out.printf( "%.8f%n" , calculatePI(PI, n, sign));
}
}
|
Python3
import math
def calculatePI(PI,n,sign):
for i in range ( 0 , 1000000 ):
PI = PI + (sign * ( 4 / ((n) * (n + 1 ) * (n + 2 ))))
sign = sign * ( - 1 )
n + = 2
return PI
PI = 3
n = 2
sign = 1
a = calculatePI(PI, n, sign)
print ( "The approximation of Pi is " ,end = "")
print ( round (a, 8 ));
|
C#
using System;
class GFG {
static double calculatePI( double PI, double n,
double sign)
{
for ( int i = 0; i <= 1000000; i++) {
PI = PI + (sign * (4 / ((n) * (n + 1)
* (n + 2))));
sign = sign * (-1);
n += 2;
}
return PI;
}
public static void Main()
{
double PI = 3, n = 2, sign = 1;
Console.Write( "The approximation of Pi is " );
Console.Write(Math.Round(calculatePI(PI, n, sign), 8));
}
}
|
Javascript
<script>
function calculatePI( PI , n , sign)
{
for (let i = 0; i <= 1000000; i++) {
PI = PI + (sign * (4 / ((n) * (n + 1)
* (n + 2))));
sign = sign * (-1);
n += 2;
}
document.write( "The approximation of Pi is " + PI);
}
let PI = 3, n = 2, sign = 1;
calculatePI(PI, n, sign);
</script>
|
Output
The approximation of Pi is 3.14159265
Time Complexity: O(N * logN * loglogN), Where N is the number of iterations
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...