# 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.

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.

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 (a2 + b2 + 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.

Method: 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π ∗ ( 1 )2 = π . Now for a very large number of generated points,2    i.e.4 AreacircleAreasquareNumber of points generated inside cicle

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 π.

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 π.

You may want to find how Python generate pseudo random numbers using

random module.