CMU 15-112 Summer 2019: Fundamentals of Programming and Computer Science
Homework 1-2 (Due Wed 3-Jul, at 5pm)
- This assignment is SOLO. This means you may not look at other student's code or let other students look at your code for these problems. See the syllabus for details.
- To start:
- Download hw1-2.py to your 'week1' folder
- Edit hw1-2.py using Pyzo
- When you are ready, submit hw1-2.py to Autolab. For this hw, you may submit up to 20 times (which is way more than you should require), but only your last submission counts.
- Do not use string indexing, lists, or recursion in this assignment.
- Do not hardcode the test cases in your solutions.
- Piazza Post [5 pts]
Ask a question on Piazza about the homework or the course. - countMultiplesOfSeven [15 pts]
Write the function countMultiplesOfSeven(a, b) that takes two integers, a and b, and returns the number of multiples of seven that occur between a and b (including a and b). For example, countMultiplesOfSeven(4, 16) would return 2, since two multiples of 7 (7 and 14) occur within that range. - nthCircularPrime [45 pts]
A circular prime is an integer number with the property that any rotation of that number's digits is prime. In this case, rotation refers to cycling the digits of a number; for example, the rotations of 1234 are 1234, 2341, 3412, and 4123. You can read more about this on the Wikipedia page. Single-digit primes are all circular, of course. To find the nth circular prime, you'll need to write isPrime and three other functions:
- rotateNumber [10 pts]
This function takes a non-negative integer number, x, and rotates that number's digits by one place. This would turn the number 1234 to 4123. - isCircularPrime [25 pts]
This function takes a non-negative integer number, x, and determines whether that number is a circular prime. To do this, you'll need to check whether every rotation of the number is prime. - nthCircularPrime [10 pts]
This function takes a non-negative integer number n, and returns the nth circular prime.
- rotateNumber [10 pts]
- carrylessAdd [35pts]
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. Here are a couple examples:carrylessAdd(785, 376) returns 51 carrylessAdd(865, 23) returns 888
Make sure you handle the case where the numbers are different lengths!