It's an important number.It is used to calculate circles and spheres, as well as measure angles using radians.Being irrational is one of's interesting properties.It has many digits that don't match a pattern.You can approximate with different methods.If you want a lot of digits, you need to do it manually.It's easy to write a computer program for you.It is a good way to learn more about the number.Learn how to calculate with basic Python programs.
Step 1: It's a good idea to install Python.
This is the programming language that will be used.You have to write the code differently and use other libraries if you want to use similar concepts in most other programming languages.
Step 2: Understand the Nilakantha series.
The Nilakantha series begins with: =3+42344456+4678489Begin with 3 as an answer and a number n/2 as a displaystyle.Add or subtract from the answer the result of a calculation.For a specified amount of times, repeat.Return and show the answer.
Step 3: A new text file is created.
You can use either a text editor or any of the other options.Your computer will recognize your file as a Python program file if you give it the extension.py.
Step 4: The decimal module should be imported.
If you don't use Python or similar libraries, precision will be limited to 17 digits.You can have arbitrary precision for the digits with this module.You don't need to install it separately because it is a default library.From decimal import.
Step 5: The digit precision should be set.
It depends on how many digits you want to calculate.To calculate 100 digits of, add the line: getContext.
Step 6: The Nilakantha series has a function.
You can imagine a function that takes the amount of iteration, calculates the series with that amount, and returns the approximation of.The function will have a structure called def nilakantha.
Step 7: The variables' starting values should be set.
The initial answer is 3.You want the high precision provided by the decimal library if you make it a Decimal.Set the variable op to 1.The variable will be used to alternate between addition and subtraction.There will be a return answer if the answer is Decimal(3.0) op.
Step 8: It's time to add a for-loop.
Initially, the for-loop will set a variable to 2.It will increment the value of n by 2 and repeat the process until the upper limit is reached.def nilakantha(reps): answer is 1 for n in range and 2 for 2.
Step 9: Add an element of the Nilakantha series to the answer.
It is enough to make one part of the fraction a Decimal.If you program the formula, you can also multiply it.In the first cycle, op is set to 1.Later, it will be set to other values.The result is 4/decimal for n in range.
Step 10: Multiply op by one.
It will be -1 if op is 1.That will make it 1.Adding a negative number is the same as subtracting a positive number.The program alternates between addition and subtraction.For n in range, the result is 4/decimal.
Step 11: An interface is needed for the function.
You will most likely want a way to input how many iteration of the series should be used, and to display the approximation that you calculated.How many times?If you haven't memorised many digits of, you might want to display the actual beginning to compare with your result.If you need more digits, add the following line.
Step 12: You should check your code.
The entire code should now look like this.Int(input) print(nilakantha)(repetitions)
Step 13: You should run your program.
Click on the "Run" symbol.Press F5 in Python's IDLE.Save your file and run it with Python if you're working in a text editor.It's best to start with 100.You can see if the program works.If you want many digits of, be prepared to wait.It takes approximately 30 seconds for this series to give you 18 digits of correctly.
Step 14: Understand the Monte-Carlo method.
Imagine a square with any length, and inside it a quarter of a circle with the same radius.Random points will be generated inside the square, and then the program will check to see if they are also in the circle.The area of the quarter-circle will be divided by the amount of points inside the square.So, because of: a quarter circle a squareThis isn't an efficient method.You will have to wait a long time to get the same amount of digits as the Nilakantha series.At the cost of even slower performance, it is a method that is easy to imagine and visualize.
Step 15: The modules need to be imported.
They all come with Python, so you don't need to install them.Random is a function for generating random numbers.The square root is a mathematical function that can be used to calculate the distance of a point.The program will be drawn by a turtle.It will be slower, but it can help understand the method and be interesting to watch.If you want to calculate quickly, you should use a different method.Random import of math and turtle.
Step 16: Ask the user how many points they want to calculate.
This can be done with the following code: print("Insert number of points:")
Step 17: The turtle needs to be made faster.
The turtle is not as fast as it could be.The turtle's speed should be set to the fastest.
Step 18: Take a picture of the situation.
Draw the coordinate system in which the rectangle and the quarter-circle are.You only need one variable, because this is the same number, to store the length of the square and the quarter- circle.If you change the size of the quarter-circle and square, this will save you a lot of time.You need to draw the coordinates of the circle and the square.The code is long, but it only moves the turtle around to draw these things.#draw y axis turtle. forward(length + 40) turtle, left, and right, respectively.
Step 19: You'll need to make a loop for the calculations.
Set the amount of dots inside the circle to 0.
Step 20: You can get a random position.
The numbers x and y are random.The center of the quarter- circle was left in the previous steps.You need both numbers to be between 0 and the length of the square.The random.uniform function can be used to get such numbers.
Step 21: Check to see if the dot is inside the circle.
You need to calculate the distance between the dot and the centre to see if it's equal to the quarter-circle.Pythagoras' Theorem is used to calculate the distance.It is:dThe formula is simpler if you use the coordinates from the previous step.Set the colour of a dot inside the circle to red and the dot outside to blue for a better overview.If the length is inside and the color is turtle.pencolor, then the turtle is blue.
Step 22: Take the dot and draw it.
Use the turtle for this: #draw dot turtle.penup.
Step 23: The results should be displayed after the loop ends.
Tell the user how many points were inside the circle and what the total amount of points was.
Step 24: The user must click on the screen to exit.
The turtle module has an exitonclick function.When the calculations are done, the window with the drawing would close and the user wouldn't have time to look at it.The line is added: turtle.exitonclick.
Step 25: You should check your code.
Your entire code should be: import random import math import turtle print.
Step 26: You should run your program.
You can click on the "Run" symbol.Press F5 in Python's IDLE.Save your file and run it with Python if you are working in a text editor.Start with a small amount of dots.You will be able to see if the program works.Be prepared to wait a long time.The calculation of 1000 points takes a long time.112 minutes and gives a few digits.Calculating 10000 points takes 15 minutes.