COL100 Assignment 2
COL100 Assignment 2
2. Analysis of correctness and efficiency must be carried out for all nontrivial helper func-
tions as well as for the main function. Any function that performs recursion counts as
nontrivial, as does any function that calls a nontrivial function.
4. For the programming component of each question, the name and the type of the main
function must be exactly as specified in the question.
5. Any function that has integer input and output must not perform any intermediate
computation using reals.
For example, if 𝑛 = 20, a possible answer is (2, 5, 13), since 2, 5, 13 are all prime and 2+5+13 = 20.
Any permutation of these three numbers — or of any other triple of primes that add up to 𝑛,
such as (2, 7, 11) — would also be acceptable.
1. Design a recursive algorithm to find three primes which add up to a given number 𝑛 > 5,
or return (0, 0, 0) if none exist. Prove the correctness of your algorithm.
2. Analyze the time and space complexity of your algorithm. For full marks, your algorithm
should take 𝑂 (𝑛 2.5 ) time.
1
3. Implement your algorithm as a function findPrimes : int → int ∗ int ∗ int.
Then the maximum possible value is 1100, obtained by choosing items 1, 3, and 5.
1. Design a recursive algorithm to find the maximum possible value for any set of items
(described by 𝑛, 𝑣, and 𝑤) and any weight limit𝑊 . Prove the correctness of your algorithm.
2. Analyze the time and space complexity of your algorithm. This is a hard problem in the
general case, so you are not expected to get a polynomial-time algorithm; you just have
to correctly analyze its complexity.
3 Human-friendly units
We often want to convert a raw measurement expressed in one unit into a human-readable
form using a combination of large and small units. For example, it is easier to understand a
height of 64 inches as “5 feet 4 inches”, and a duration of 106 seconds as “11 days 13 hours 46
minutes 40 seconds”. In this format, the number of each unit must be less than the size of the
next larger unit (e.g. it would not be valid to say “4 feet 16 inches”, because 16 inches ≥ 1 foot).
Suppose you are given two functions specifying the names of the units and their conversion
2
factors, for example
Interpret a factor of zero to mean that there are no higher units to convert to. With these
functions, for example, an input of 3602 should be converted to “1 hours 0 minutes 2 seconds”
or “1 hours 2 seconds” (either is acceptable).
2. Design an iterative algorithm for the same problem, which requires only 𝑂 (1) deferred
computations / stack frames (no matter how many units are needed). Prove this fact.
3. Analyze the time complexity of your algorithms, assuming that: (i) size(name(𝑛)) = 𝑂 (1)
and factor (𝑛) = 𝑂 (1), and (ii) the cost of string concatenation (∧) is 𝑂 (𝑛 + 𝑚) where 𝑛
and 𝑚 are the sizes of the two strings being joined.
In the lectures, we have discussed a fast recursive function for this problem, namely intSqrt2,
which computes the integer square root of 𝑛 in 𝑂 (log 𝑛) time. However, it also requires 𝑂 (log 𝑛)
space to do so.
1. Design a fast iterative algorithm for this problem, which has a time complexity of 𝑂 (log 𝑛),
but a space complexity of only 𝑂 (1). Prove the correctness of your algorithm.
Hint: Consider the invariant 𝑖 2 ≤ b𝑛/4𝑝 c < (𝑖 + 1) 2 , for some natural numbers 𝑖 and 𝑝.
2. Prove the 𝑂 (log 𝑛) time complexity and 𝑂 (1) space complexity of your algorithm.
3
3. Implement your algorithm as a function intSqrt : int → int. Verify that it succeeds
quickly even for large numbers, like 400,000,000.
1. One file should be a PDF file containing all your written work, i.e. mathematical definitions
of algorithms, correctness and efficiency analysis, etc. This can be handwritten with pen
and paper and then scanned with your mobile, or it can be typed on your device using
MS Word or LaTeX or any other software. The only requirement is that it should be
clearly legible.
2. The other file should be an SML file containing all your code. Put your solutions to all four
programming problems into a single file, along with any helper functions they require.
We should be able to run any of the four required functions by typing in a function call
at the bottom of the file.
The filenames of your submitted files should be exactly the same as your entry number. For exam-
ple, if your entry number is 2017CS10389, you should name your SML file as 2017CS10389.sml
and your PDF file as 2017CS10389.pdf. Your submission should consist of only these two files,
uploaded individually. There should be no sub-folders or zip files in the submission on Moodle.
Please ask any queries related to the assignment on the COL100 Piazza page (https://piazza.
com/iit_delhi/fall2020/col100).