首页 > > 详细

辅导CSC 120、辅导Canvas留学生、讲解Python程序设计、Python语言讲解 解析R语言编程|讲解留学生Processing

CSC 120: Introduction to Computer Programming for Non-majors
Spring 2019
Assignment 4
Notes:
For each question create a Python script with the name Question xx
where xx is the question number.
Provide your name and what program does in each script.
Provide necessary comments.
Once you are done, create a folder with the name Assignment-03, put all
your scripts in there, zip it, and then upload the zip file in to Canvas.
If you are not clear about any of these steps, just ask.
This must be your own work. I have already explained policies regrading
plagiarism and they are in your syllabus.
Question 1 (20 points): Write a program that inputs a non negative integer,
separates the integer into its digits and prints them separated by tabs each.
For example, if the user types in 42339, the program should print:
4 2 3 3 9
Note: The non negative integer has an arbitrary length. You should use a
paper and a pencil to figure out the algorithm. Think about how to exploit
integer division and remainder operator.
Question 2 (20 points): Write a program to determine all pairs of positive
integers, (a, b), such that a < b < n and (a
2 + b
2 + 1)/(ab) is an integer.
Here n is an integer and n > 1.
Question 3 (40 points) Write a program to approximate the area under a
circle with radius r. Note that you should forget the existence of the well
known formula area = πr2.
1Method: The equation of a circles with radius r, centered at origin is
x2 + y2 = r2. Divide the area under the top of half (above x axis) in to
small rectangles of width of your choice – smaller the better and you should
pass this as a parameter to your method– and add these areas of all these
rectangles to approximate the area of the upper half of the circle.
Multiplying that value by 2 give the approximate area of the circle.
You must test your results with known radius values (Say, if you set your
radius to 1. Then you should see the π as the answer for the area).
You don’t need any Calculus knowledge to solve this problem.
Question 4 (20 points) Monte Carlo methods are a broad class of
computational algorithms that rely on repeated random sampling to obtain
numerical results. One of the basic examples of getting started with the
Monte Carlo algorithm is the estimation of Pi.
Basic Idea: The idea is to simulate random (x, y) points in a 2-D plane
with domain as a square of side 1 unit. Imagine a circle inside the same
domain with same diameter and inscribed into the square. We then
calculate the ratio of number points that lied inside the circle and total
number of generated points. Refer to the image below:
We know that area of the square is 1 unit sq while that of circle is. Now for a very large number of generated points,
Areacircle
Areasquare=
Number of points generated inside cicle
Total number of points generated i.e.
π = 4 ×
Number of points generated inside cicle
Total number of points generated
The beauty of this algorithm is that we don’t need any graphics or
simulation to display the generated points. In randomized and simulation
algorithms like Monte Carlo, the more the number of iterations, the more
accurate the result is. Thus, it is “estimating the value of π” and not
“calculating the value of π”. Implement this algorithm in Python and
estimate the value of π.
2You may want to find how Python generate pseudo random numbers using
random module.

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!