Log in to save your progress.
<< Back to problems
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Calculate pi with the Gottfried Leibniz formula

Prerequisites: Basic course part I

According to Gottfried and Leibniz, it is possible to calculate pi using the following formula: $$\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - ...$$ Let the user choose the number of terms as input. Then print what the approximate value of pi will be. In the formula above, five terms are shown. Don't forget the 4!

Feel free to experiment with your program. How many terms do you need to get two decimal places of accuracy on pi? Five decimals? $$\pi = 3.1415926535...$$ Above, pi is shown with the first ten correct decimals.

Example input 1

1

Example output 1

4.0

Example input 2

5

Example output 2

3.33968253968254

Login to save your progress and your code.

Create

Write your code here.

-- Output from your program will be here --