00:01
So we're going to write a program that approximates the value of pi by summing the terms for this series.
00:06
4 divided by 1, minus 4 divided by 3, plus 4 divided by 5, and so on.
00:12
So we're alternating plus, minus, plus, minus.
00:15
And our numerator is always 4, and our denominator goes from 1, 3, 5, 7, 9, 11.
00:21
We're going to ask the user how many terms they want to use.
00:26
And then we will sum the first n terms of the series for how many they ask.
00:32
Then we're going to use the program subtract our approximation from math .py to see just how accurate it is.
00:39
So here's what i see.
00:41
Plus 4 over 1, minus 4 over 3, plus 4 over 5, and so on.
00:45
So we're going to use some sort of counter, a for loop, to go from 0 up through our n minus 1 term.
00:56
So we have n terms, but we want to start counting at 0 because that's what computers do, 0 indexing.
01:02
So let's say they want six terms we're gonna have 0 1 2 3 4 5 that's six terms how does that work with our denominator well i see this pattern 2i plus 1 2 times 0 plus 1 is 1 2 times 1 plus 1 is 3 2 times 2 plus 1 is 5 and so on if i is odd we're gonna subtract if i is even we're gonna add and we'll use remainder division to see if we have an odd or an even index up here so counting index so so let's go ahead and see what the program looks like.
01:35
So we're going to use math .py.
01:36
So i'm going to import math.
01:38
All my imports should happen at the top of the program.
01:41
I'm going to get some input.
01:43
I'm going to ask the user, how many terms do you want to sum? i'm going to cast that to be an integer.
01:49
And i'm going to assign it to a variable named number of terms.
01:54
I'm going to set up my approximate pi variable, initialize it to zero.
02:00
And i'll be adding to it as we go along.
02:02
So based on, and we're going to, we don't have any error checking here.
02:05
We're going to assume that our user is smart enough to know that they have to enter a number for this.
02:09
If they don't, we'll get an error.
02:11
It doesn't seem like the textbook has gotten to error checking yet...