The Leibniz formula is a way of calculating the value of pi

The Leibniz formula is a way of calculating the value of pi. Its definition in Wikipedia is:



Write a program in Python that contains a function which calculates pi per the Leibniz formula. The program must use that function to calculate and print the value of pi based on the first 12 values. (The correct answer is 3.058402765927333.) Be sure to include a header block and to use comments in program.

Solution

Please find the required program and output below: Please find the comments against each line for the description:

import math # This will import math module


def calculate_pi():   # function that return value of m(i) for input i
   sum = 0  
   for j in range(0,12):   #iterate loop till 12 times
       sum = sum + (math.pow(-1,j)/((2*j)+1))   #summation of the Leibniz series
   pi = 4 * sum  
   return float(pi)   #return number
  
  
#print the output
print (\"pi = \", calculate_pi())

--------------------------------------------------------------------

OUTPUT:

The Leibniz formula is a way of calculating the value of pi. Its definition in Wikipedia is: Write a program in Python that contains a function which calculates

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site