This method for calculating the square root of a number n starts by making a (non zero) guess at the square root. It then uses the original guess to calculate a new guess, according to the formula
newGuess = ((n / oldGuess) + oldGuess) / 2.0;In your function sqrt() have two variables oldGuess and newGuess. Initialize oldGuess to n / 2.0 and calculate newGuess according to the above formula. Use a while loop to iterate as long as the absolute value of the difference between the oldGuess and newGuess is greater than 1.0E-06. Do not forget to reset the value of oldGuess to the newGuess value in the while loop.
The structure of your program is as follows:
def sqrt (n): ... def main (): ... main ()
In your function main() you will prompt the user to enter a positive number. If the number is negative, prompt the user again and again. For a positive number, calculate the square root using the your function sqrt(). Find the difference between the square root you obtained and the value obtained from using the exponentiation operator. Write out the value the user entered, the square root you computed, and the difference (your square root - n ** 0.5). Your session will look like this:
Enter a positive number: 12 Square root is: 3.46410161514 Difference is: 0.0
There is a piece of trivia associated with this algorithm. Professional mathematicians are familiar with this algorithm as Newton's method. But the algorithm is much older than Newton and was known in several cultures. In first century AD, the Greek mathematician Heron of Alexandria used this method to determine the square root of 720. There is evidence that this method goes even further back, as far as the Babylonians. In the Yale University Babylonian Collection, there is a cuneiform tablet that seems to use this method to calculate the square root of 2. The beauty of this algorithm is its rapid convergence to the square root the number.
The program that you will be writing will be called CalcSqrt. We will be looking at good documentation, and adherence to the coding convention discussed in class. You may use the same variable names used in the problem statement. Your file CalcSqrt.py will have the following header:
# File: CalcSqrt.py # Description: # Student Name: # Student UT EID: # Course Name: CS 303E # Unique Number: # Date Created: # Date Last Modified:
Use the turnin system to submit your CalcSqrt.py file. The proctors should receive your work by 11 PM on Friday, 19 October 2012. There will be substantial penalties if you do not adhere to the guidelines. The TA in charge of this assignment is Seonggu Huh (email@example.com).
There is another novel approach to calculate π. Imagine that you have a dart board that is 2 units square. It inscribes a circle of unit radius. The center of the circle coincides with the center of the square. Now imagine that you throw darts at that dart board randomly. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. The area of a circle with unit radius is just π square unit. The area of the dart board is 4 square units. The ratio of the area of the circle to the area of the square is π / 4.
To simuluate the throwing of darts we will use a random number generator. The Random module has several random number generating functions that can be used. For example, the function uniform(a, b) returns a floating point random number in the range a (inclusive) and b (exclusive).
Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.
A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. The way we achieve that is:
xPos = random.uniform (-1.0, 1.0) yPos = random.uniform (-1.0, 1.0)
To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is math.hypot (xPos, yPos). The radius of the circle is 1 unit.
The program that you will be writing will be called CalculatePI. It will have the following structure:
import math import random def computePI ( numThrows ): ... def main (): ... main()Your function main() will call the function computePI() for a given number of throws. The function computePI() will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will determine if that randomly generated point is inside the circle or not. You will do this as many times as specified by the number of throws. You will keep a count of the number of times a dart lands within the circle. That count divided by the total number of throws is the ratio π/4. The function computePI() will then return the computed value of PI.
In your function main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. You will compare your result with the value given by math.pi. The quantity Difference in the output is your calculated value of PI minus math.pi. Use the following number of throws to run your experiment - 100, 1000, 10,000, 100,000, 1,000,000, and 10,000,000. You will call the function computePI() with these numbers as input parameters. Your output will be similar to the following, i.e. the actual values of your Calculated PI and Difference will be different but close to the ones shown:
Computation of PI using Random Numbers num = 100 Calculated PI = 3.320000 Difference = +0.178407 num = 1000 Calculated PI = 3.080000 Difference = -0.061593 num = 10000 Calculated PI = 3.120400 Difference = -0.021193 num = 100000 Calculated PI = 3.144720 Difference = +0.003127 num = 1000000 Calculated PI = 3.142588 Difference = +0.000995 num = 10000000 Calculated PI = 3.141796 Difference = +0.000204 Difference = Calculated PI - math.piYour output must be in the above format. The number of throws must be left justified. The calculated value of π and the difference must be expressed correct to six places of decimal. There should be plus or minus sign on the difference.
The program that you will be writing will be called CalculatePI. We will be looking at good documentation, and adherence to the coding conventions discussed in class. Your file CalculatePI.py will have the following header:
# File: CalculatePI.py # Description: # Student Name: # Student UT EID: # Course Name: CS 303E # Unique Number: # Date Created: # Date Last Modified:
Use the turnin system to submit your CalculatePI.py file. The proctors should receive your work by 11 PM on Friday, 19 October 2012. There will be substantial penalties if you do not adhere to the guidelines. The TA in charge of this assignment is Seonggu Huh (firstname.lastname@example.org).