Description
Read this first. A few things to bring to your attention:
2. Make sure you back up your work! I recommend, at a minimum, doing your work in a Dropbox folder or, better yet, using git, which is well worth your time and effort to learn.
Instructions on writing and submitting your homework.
When I extract your compressed file, the result should be a directory, also called yourUniqueName_hwX. In that directory, at a minimum, should be a jupyter notebook file, called yourUniqueName.hwX.ipynb, where again X is the number of the current homework. You should feel free to define supplementary functions in other Python scripts, which you should include in your compressed directory. So, for example, if the code in your notebook file imports a function from a Python file called supplementary.py, then the file supplementary.py should be included in your submission. In short, I should be able to extract your archived file and run your notebook file on my own machine. Please include all of your code for all problems in the homework in a single Python notebook unless instructed otherwise, and please include in your notebook file a list of any and all people with whom you discussed this homework assignment. Please also include an estimate of how many hours you spent on each of the sections of this homework assignment.
1 Warm up: Defining Simple Functions (2 points)
In this problem, you will get practice defining simple functions in Python.
1. Define a function called say_hi, which takes no arguments and prints the string Hello, world! when called.
2. Define a function called goat_pad, which takes a string as its only argument, and prints that string, prepended and appended with the string goat. So, goat_pad(’bird’) should produce the output goatbirdgoat
goat_pad(’_’) should produce the output
goat_goat
2 Euclid’s algorithm (2 points)
Euclid’s algorithm (https://en.wikipedia.org/wiki/Euclidean_algorithm) is a method for finding the greatest common divisor (GCD) of two numbers. Recall that the GCD of two numbers m and n is the largest number that divides both m and n.
2. Use your function to evaluate the GCDs of the following pairs of numbers:
(b) 1200, 300
(c) 5040, 60
3. What does your function do if one or both of its arguments are negative? Does thisbehavior make sense?
3 Approximating Euler’s number e (3 points)
The base of the natural logarithm, e, is typically defined as the infinite sum
(1)
where k! denotes the factorial of k,
k! = k · (k − 1) · (k − 2) · ··· · 3 · 2 · 1,
where we define 0! = 1 by convention. For more on Euler’s number, see https://en. wikipedia.org/wiki/E_(mathematical_constant). In this problem, we will explore different approaches to approximating this number.
. (2)
3. Define a function called euler_approx that takes a single argument, a float epsilon, and uses the sum in (1) to obtain an approximation of e that is within epsilon of the true value of e. Hint: use a while-loop. Note: you can use the Python math module to get the true value of e (up to floating point accuracy): math.exp(1).
4. Define a function called print_euler_sum_table that takes a single positive integer n as an argument and prints the successive values obtained from euler_infinite_sum(k) as k ranges from 1 to n, one per line.
5. Which of these two approximations is better?
4 Testing Properties of an Integer (3 points)
In this problem, you’ll get a bit more practice working with conditionals, and a first exposure to the kind of thinking that is required in a typical “coding interview” question. A positive integer n is a power of 2 if n = 2p for some integer p ≥ 0.
2. Generalize your previous solution to a function is_power that takes two positive integers as its arguments, b and n, in that order, and returns a Boolean. is_power(b,n) should return True if n is a power of b and False otherwise.




Reviews
There are no reviews yet.