Begin with 3 as “answer”, and a number n=2{\displaystyle n=2} Calculate 4n∗(n+1)∗(n+2){\displaystyle {\frac {4}{n*(n+1)*(n+2)}}}. Add or subtract the result of that calculation from the answer. Repeat for a specified amount of times. Return and display the answer.
In the first cycle, op is set to 1, so multiplying with it does nothing. But it will be set to other values later.
If you haven’t memorized many digits of π, you may also want to display the actual beginning of π to compare with your result. If that is the case, add the following line: print(“3. 1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679”) (If you need more digits of π for your comparison, you can copy them from the internet. )
Start with a small amount of iterations, like 100. This will let you see whether the program works. Be prepared to wait if you want many digits of π. For example, doing a million iterations of this series gives you 18 digits of π correctly, and it takes approximately 30 seconds.
With a lot of points, dividing the amount of points inside the quarter-circle by the amount of points inside the square will be like dividing the area of the quarter-circle by the area of the square. So, because of:AquartercircleAsquare=14πr2r2=14π{\displaystyle {\frac {A_{quartercircle}}{A_{square}}}={\frac {{\frac {1}{4}}\pi r^{2}}{r^{2}}}={\frac {1}{4}}\pi }You can calculate π with:4AquartercircleAsquare=π{\displaystyle 4{\frac {A_{quartercircle}}{A_{square}}}=\pi } The program can’t just use the area directly because calculating the area of the quarter-circle would require π, which this program is supposed to determine. This is not an efficient method. You will have to wait quite long to get the same amount of digits of π as, for example, the Nilakantha series. However, it is a method that is easy to imagine and visualize (at the cost of even slower performance).
First, define a variable that stores the length of the square and the radius of the quarter-circle in pixels (you only need one variable, because this is the same number). This will save you a lot of work if you decide to change the size of the quarter-circle and square. length = 300 # radius of circle and length of the square in pixels Then, you need to actually draw the coordinate axes and the circle. This code is long, but all it does is move the turtle around to draw these things. #draw y axis turtle. pensize(2) turtle. forward(length + 40) turtle. left(135) turtle. forward(20) turtle. back(20) turtle. left(90) turtle. forward(20) turtle. penup() turtle. home() turtle. pendown() #draw x axis turtle. left(90) turtle. forward(length + 40) turtle. left(135) turtle. forward(20) turtle. back(20) turtle. left(90) turtle. forward(20) turtle. penup() turtle. goto(0,length) turtle. left(45) turtle. left(180) turtle. pendown() #draw quarter of circle turtle. pencolor(“red”) turtle. circle(length,-90)
To calculate the distance, you need to use Pythagoras’ theorem. It is:d=(x2−x1)2+(y2−y1)2{\displaystyle d={\sqrt {(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}}}}However, since the centre is located at (0,0), x1 and y1 are both 0 and can be ignored. The formula is simpler:d=x22+y22{\displaystyle d={\sqrt {{x_{2}}^{2}+{y_{2}}^{2}}}}In Python code (x2 and y2 are the coordinates that you got in the previous step): #determine distance from center d = math. sqrt(x2 + y2) If the point is inside the circle, increase the variable that counts the points inside the circle by 1. For better overview, set the colour of a dot inside the circle to red and of a dot outside the circle to blue. if d <= length: inside += 1 turtle. pencolor(“red”) else: turtle. pencolor(“blue”)
Start with a small amount of dots, like 100. This will let you see whether the program works. Be prepared to wait very long. Even calculating 1000 points takes approx. 1½ minutes, and gives a few (1–2) digits of π. Calculating 10000 points takes 15 minutes, and gives 2–3 digits of π.