# 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, (

*), such that*

*a, b**and (*

*a < b < n*

*a*^{2}+

*b*^{2}+ 1)

*(*

*/**) is an integer. Here*

*ab**is an integer and*

*n**1.*

*n >*** Question 3 (40 points) **Write a program to approximate the area under a circle with radius

*. Note that you should forget the existence of the well known formula*

*r**=*

*area*

*πr*^{2}.

** Method: **The equation of a circles with radius

*, centered at origin is*

*r**x*^{2} + *y*^{2} = *r*^{2}. 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 (

*) 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:*

*x, y*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*

^{π}__Area__

*Area*

*circle**Number of points generated inside cicle*

*square*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.