CMU 15-112 Fall 2016: Fundamentals of Programming and Computer Science
Lab 2 (Due Saturday 10-Sep, at 6pm)
- This lab is Collaborative.
See lab1 and the syllabus for details.
- Starter files:
lab2.py
and
cs112_f16_wk2.py
- This week you may use up to 10 submissions (which is still plenty more than you should need). Only your last submission counts.
- Do not use strings, lists, or recursion this week.
- Do not hardcode the test cases in your solutions.
- nthEmirpsPrime(n)
Write the function nthEmirpsPrime(n) that
takes a non-negative int n and returns the nth "Emirp prime", where an Emirp prime is a prime number which becomes a different prime number when its decimal digits are reversed. For example, 13 is an Emirp prime because when we reverse the digits we get 31, which is also prime. 2, 3, 5, 7, and 11 are all examples of primes that are not Emirp primes because they are the same prime when the digits are reversed. Here are the first several Emirp primes:
13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199,...
So nthEmirpsPrime(0) returns 13.
Aside: in case you missed it, emirp is prime spelled backwards. Also, we didn't make this up (check out
"emirp" on Wikipedia)!
- findZeroWithBisection(f, x0, x1, epsilon)
Write the function findZeroWithBisection(f, x0, x1, epsilon) as described here.
- carrylessAdd(x, y)
First, read the first page (page 44) from
here about Carryless Arithmetic. Fun! Then, write the function carrylessAdd(x, y) that takes two non-negative integers x and y and returns their carryless sum. As the paper demonstrates, carrylessAdd(785, 376) returns 51.
- nthKaprekarNumber(n)
Write the function nthKaprekarNumber(n) that takes a non-negative int n and returns the nth Kaprekar number, where a Kaprekar number is a non-negative integer, the representation of whose square can be split into two parts (where the right part is not zero) that add up to the original number again. For instance, 45 is a Kaprekar number, because 45**2 = 2025 and 20+25 = 45.
- integral(f, a, b, N)
Write the function integral(f, a, b, N) as described
here (see #4).