Homework 02

Due Tue, Feb 18, 11:59pm

Download the .ipynb file for this notebook, and place your solutions where indicated (you can use multiple cells for each problem). Please name the file HW02_lastname_firstname.ipynb. Then upload it to Blackboard as Homework 2 in the Assignments tab. See Collaboration Policy in Homework section of course webpage. Please include comments in your code to make it easier to read.

Problem 1

Write a function that takes in an integer and determines whether it is a prime (i.e. has no divisors other than 1 and itself). There is a built in function is_prime that does this, but don’t use it or any other built-in functions. Use your function to determine whether $2^{19} − 1$ is prime. Include in your Jupyter notebook a short written explanation of the algorithm you are using to test whether numbers are prime.

In [2]:
# Put your solution here (you can add more cells using the Insert menu)

Problem 2

Write a function that takes as input an integer $n$ and returns its factorization into prime numbers. The output should be a list of the prime factors, with each prime $p$ appearing $k$ times in the list where $k$ is the largest power of $p$ such that $p^k$ divides $n$. The order of the elements of the list does not matter. For instance, if $n=12$, any of [2,2,3], [2,3,2], or [3,2,2] would be vaild outputs. Do not use the built-in factor function. Test your function on $2^{22}-1, 2^{23}-1$, and $2^{24}-1$.

In [2]:
# Solution here

Problem 3

(a) Write a function that takes as input a string of characters, and returns the characters in reverse order (either as a list of characters, or as a string). Try your function on "MADWARWOLF". It should return "FLOWRAWDAM" (or a list of these characters).

(b) Write a function that takes as input a string of characters, and applies the above reverse cipher, followed by a shift (Caesar) cipher like the one we did in class (shifting by an amount of your choice). How hard would it be to break this combination cipher?

In [ ]:
# Solution here

Problem 4

I made a string of capital letters by starting with some lyrics of a song written in English and stripping out all spaces and punctuation. I then encrypted this string with the shift (Caesar) cipher, obtaining the string below. What string did I start with, and what key did I use?

EVMVIKYFLXYKZUSVFERSFRKZKJRSZXSCLVNRKVIPIFRUGFJVZUFECFFBRKDVEVMVIKYFLXYKZUJVVKYVURPNYVERSZXSFRKTFDZEXDPNRP

In [4]:
# Solution here

Problem 5

Find several paragraphs of text written in English (eg from a book, magazine, news article, email exchange, etc). Strip out all non-letter characters (spaces, punctuation, numbers, etc), and convert all remaining letters to upper-case. Call this string CORPUS1. Ideally it should have at least 500 characters. Determine the relative frequency of each letter in CORPUS1. Your output should be a list of the frequencies of each of the 26 letters.

In [5]:
# Solution here