00:02
The german mathematician gottfried leibniz developed the following method to approximate the value of pi.
00:11
Pi over 4 equal 1 minus 1 3 3rd plus 1 5th minus 1 7th plus and so to infinity.
00:21
So it's a series that converge to the number of pi 4th.
00:26
We want to write a program that allows the user to specify the number of iterations that is the number of terms in this series that we got to use to find the approximation of pi and that displays the result of the approximation.
00:48
So the first thing we got to say here is that to write the approximation of pi using this expression here, we see that the terms that agadabis add together are alternating in sign.
01:09
That is, we start by 1, which is positive, then 1 third that is negative, 1 5th is positive, 1 over 7 is negative, and so on.
01:23
And the other thing is that the numbers are the reciprocals of the odd natural numbers.
01:31
That is, for example, here, this 1 is 1 over 1.
01:36
This is negative over 3 this is plus this plus here 1 over 5 this is negative over 7 so we have the reciprocals of the odd natural numbers and the sign is alternating so knowing that if we start with index 0 here let's say k equals 0 then the corresponding odd number will be 2k plus 1 which is 1 in this case.
02:14
For k equal 1 we get 2k plus 1 is here is 1 here is 3 which is the denominator here.
02:26
Then for k equal 2 2 k plus 1 is 2 times 2 plus 1 is 5 which is the denominator here and so on.
02:36
For k equal 3 2 k plus 1 is 7.
02:41
So the denominator can be found by using the index k starting at zero through the equation to k plus one now the sign of each turn this case is a power of negative one and if we take the power of k of negative one for k equals zero we get negative one to the zero is one so for k equals zero the number is positive and that's true because is one over one equal to plus one.
03:17
1.
03:19
For k -equal 1, then we get negative 1 to the rest to the power 1 is negative 1, which is the sign of 1 over 3, which is correct, and so on.
03:31
For k -equal 2, negative 2 square is positive, and so the sign corresponding to the denominator 5 is positive, which is correct, and so on.
03:45
So we can say that in general, i'm going to put it over here, here.
03:54
The terms of the series is negative 1 to the kth power over 2k plus 1.
04:02
This is a formula we got to use to reproduce any of the terms in this series.
04:11
And so we get to accumulate these terms up to the value of k that is specified by the user.
04:19
In fact, because k start at zero, if the user specified two iterations, for example, k got to be 0 and 1.
04:30
If the user specify three iterations, then k takes the values 0, 1, and 2, and so on.
04:38
So this is for k equals 0, 1, 2, etc.
04:43
So we can say that we have this general formula for the terms of the series.
04:52
At the 1 k power over to k plus 1.
04:58
So having said that, now we have here a solution, a program that solves the problem that was stated.
05:10
And the first thing is to input, to ask the user to input a string corresponding to a positive number, positive integer number, which will be the number of iterations.
05:24
And then we use a common input in python, in which we specify the string, the message that is going to put on screen for the user to know what it get to be entered.
05:43
So the string is enter the number of iterations, it's a positive integer number.
05:49
That we store in a variable called my string.
05:53
And then we try to convert that string to an integer number.
06:00
That's why we use a try -catch block here.
06:07
Try -catch, sorry, a try -except block.
06:11
And the instruction or comment in the try part is just the conversion of my string to an integer number and store that in the variable number of iterations.
06:26
If that fails, that is if my string does not contain an integer number, then we write or put the message on screen error.
06:38
The number of iterations must be a positive integer number.
06:41
Please try again.
06:42
The program is going to stop here because we put as exit common.
06:48
So if the string enter by the user does not correspond to an integer number, the program is going to put this message here, and it's going to stop execute.
07:02
So the user got to rerun the program to enter a correct number of iterations and see the approximation of pi.
07:12
But after the conversion has been successful, that is if we proceed to line 11, we still get a check that the number, the integer number is a positive number.
07:28
So if that's not the case, that is the number of iterations contains a negative or zero number, which we know already is an integer number, but is negative or zero, we again print an error message, in fact the same error message, and then exit again the program by using the exit column.
07:54
So if the string cannot be converted into an integer number, or being converted to an integer number is negative or zero.
08:04
In either case, we put an error messaging on the screen and stop running the program.
08:10
If we proceed to line 18 here, that means we have in the variable number of iterations a positive integer number.
08:19
And that's the index we are going to use to make a fall loop here.
08:27
But before that, because we're going to accumulate the terms of the series, we get to define and initialize a variable, in this case it is called pi approximation to zero.
08:41
And then we have the full loop in which the range goes from zero to the number of iterations minus 1.
08:47
Remember that the first argument of the range function is the starting index, and the second argument is the upper bound of the last index...