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++
// C++ code to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate PI double calculatePI( double PI, double n, double sign) { // Add for 1000000 terms for ( int i = 0; i <= 1000000; i++) { PI = PI + (sign * (4 / ((n) * (n + 1) * (n + 2)))); // Addition and subtraction // of alternate sequences sign = sign * (-1); // Increment by 2 according to formula n += 2; } // Return the value of Pi return PI; } // Driver code int main() { // Initialise sum=3, n=2, and sign=1 double PI = 3, n = 2, sign = 1; // Function call cout << fixed << setprecision(8) << "The approximation of Pi is " << calculatePI(PI, n, sign) << endl; return 0; } |
C
// C code to implement the above approach #include <stdio.h> // Function to calculate PI double calculatePI( double PI, double n, double sign) { // Add for 1000000 terms for ( int i = 0; i <= 1000000; i++) { PI = PI + (sign * (4 / ((n) * (n + 1) * (n + 2)))); // Addition and subtraction // of alternate sequences sign = sign * (-1); // Increment by 2 according to formula n += 2; } // Return the value of Pi return PI; } // Driver code void main() { // Initialise sum=3, n=2, and sign=1 double PI = 3, n = 2, sign = 1; // Function call printf ( "The approximation of Pi is %0.8lf\n" ,calculatePI(PI, n, sign)); } // This code is contributed by ashishsingh13122000. |
Java
// JAVA code to implement the above approach import java.util.*; class GFG { // Function to calculate PI static double calculatePI( double PI, double n, double sign) { // Add for 1000000 terms for ( int i = 0 ; i <= 1000000 ; i++) { PI = PI + (sign * ( 4 / ((n) * (n + 1 ) * (n + 2 )))); // Addition and subtraction // of alternate sequences sign = sign * (- 1 ); // Increment by 2 according to formula n += 2 ; } // Return the value of Pi return PI; } // Driver code public static void main(String[] args) { // Initialise sum=3, n=2, and sign=1 double PI = 3 , n = 2 , sign = 1 ; // Function call System.out.print( "The approximation of Pi is " ); System.out.printf( "%.8f%n" , calculatePI(PI, n, sign)); } } // This code is contributed by sanjoy_62. |
Python3
import math # Function to calculate PI def calculatePI(PI,n,sign): # Add for 1000000 terms #for (int i = 0; i <= 1000000; i++) { for i in range ( 0 , 1000000 ): PI = PI + (sign * ( 4 / ((n) * (n + 1 ) * (n + 2 )))) # Addition and subtraction of alternate sequences sign = sign * ( - 1 ) # Increment by 2 according to formula n + = 2 # Return the value of Pi return PI # Driver code # Initialise sum=3, n=2, and sign=1 PI = 3 n = 2 sign = 1 # Function call a = calculatePI(PI, n, sign) print ( "The approximation of Pi is " ,end = "") print ( round (a, 8 )); # This code is contributed by ashishsingh13122000 |
C#
// C# code to implement the above approach using System; class GFG { // Function to calculate PI static double calculatePI( double PI, double n, double sign) { // Add for 1000000 terms for ( int i = 0; i <= 1000000; i++) { PI = PI + (sign * (4 / ((n) * (n + 1) * (n + 2)))); // Addition and subtraction // of alternate sequences sign = sign * (-1); // Increment by 2 according to formula n += 2; } // Return the value of Pi return PI; } // Driver code public static void Main() { // Initialise sum=3, n=2, and sign=1 double PI = 3, n = 2, sign = 1; // Function call Console.Write( "The approximation of Pi is " ); Console.Write(Math.Round(calculatePI(PI, n, sign), 8)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // Javascript code to implement the above approach // Function to calculate PI function calculatePI( PI , n , sign) { // Add for 1000000 terms for (let i = 0; i <= 1000000; i++) { PI = PI + (sign * (4 / ((n) * (n + 1) * (n + 2)))); // Addition and subtraction // of alternate sequences sign = sign * (-1); // Increment by 2 according to formula n += 2; } // Return the value of Pi document.write( "The approximation of Pi is " + PI); } // Driver code // Initialise sum=3, n=2, and sign=1 let PI = 3, n = 2, sign = 1; // Function call calculatePI(PI, n, sign); </script> |
The approximation of Pi is 3.14159265
Time Complexity: O(N * logN * loglogN), Where N is the number of iterations
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!