0% found this document useful (0 votes)
49 views

COL100 Assignment 2

This document provides instructions for Assignment 2 of the COL100 course. It consists of 5 programming problems related to algorithms and SML implementation. For each problem, students must provide mathematical definitions, analysis of time/space complexity, and SML code. Submissions should include a PDF with written work and an SML file with code. The due date is December 22, 2020. Files must be named according to the student's entry number and contain only the required problems.

Uploaded by

ManikyaMayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

COL100 Assignment 2

This document provides instructions for Assignment 2 of the COL100 course. It consists of 5 programming problems related to algorithms and SML implementation. For each problem, students must provide mathematical definitions, analysis of time/space complexity, and SML code. Submissions should include a PDF with written work and an SML file with code. The due date is December 22, 2020. Files must be named according to the student's entry number and contain only the required problems.

Uploaded by

ManikyaMayank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COL100 Assignment 2

Due date: 22 December, 2020

General assignment guidelines


1. For each question, the algorithm must be defined mathematically in your written work,
as well as implemented in SML code in your programming component.

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.

3. Big O notation should be as tight as possible. For example, reporting 𝑂 (𝑛 2 ) complexity


for an 𝑂 (𝑛) algorithm could lose you marks, even though it is technically correct.

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.

1 Sum of three primes


It is a famous unsolved problem in number theory to prove whether every integer 𝑛 > 5 can be
expressed as the sum of three prime numbers. But we don’t need any number theory to simply
verify whether the conjecture is true for any specific number. That is, given an integer 𝑛 > 5,
we want to find three prime numbers 𝑖, 𝑗, 𝑘, not necessarily distinct, whose sum is equal to 𝑛.

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.

2 Packing the dikki


I have a set of 𝑛 different items I want to take to the market to sell. Each item has a certain
value and a certain weight, both positive integers. Unfortunately, there is a limited amount of
weight 𝑊 ∈ N that I can carry in my vehicle. What items should I take to maximize the total
value, without the total weight exceeding 𝑊 ? Assume the values and weights are given by two
functions 𝑣 : {1, . . . , 𝑛} → N and 𝑤 : {1, . . . , 𝑛} → N.

For example, suppose 𝑛 = 5 and 𝑊 = 100, with items

𝑣 (1) = 500, 𝑤 (1) = 40,


𝑣 (2) = 700, 𝑤 (2) = 80,
𝑣 (3) = 300, 𝑤 (3) = 30,
𝑣 (4) = 200, 𝑤 (4) = 50,
𝑣 (5) = 300, 𝑤 (5) = 20.

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. Implement your algorithm as a function maximumValue : int ∗ (int → int) ∗ (int →


int) ∗ int → int, which can be called as maximumValue(n, v, w, W).

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

name(0) = “seconds”, factor (0) = 60,


name(1) = “minutes” factor (1) = 60,
name(2) = “hours” factor (2) = 24,
name(3) = “days” factor (3) = 365,
name(4) = “years”, factor (4) = 0.

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).

1. Design a recursive algorithm to convert an integer 𝑛 of the smallest unit to a human-


readable string using the name and factor functions. You may also need to design a
toString function to convert a natural number to a string, e.g. toString(64) = “64”.

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.

4. Implement both algorithms as functions convertUnitsRec and convertUnitsIter


of type int ∗ (int → string) ∗ (int → int) → string. Use your own toString
function, not the built-in function Int.toString.

4 Iterative integer square root


Recall that the integer square root of a natural number 𝑛 is the unique natural number 𝑘 such
that
𝑘 2 ≤ 𝑛 < (𝑘 + 1) 2 .

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.

5 Submission and other logistics


As usual, you should submit two files to the Moodle assignment page.

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.

Failure to comply with these submission instructions will lead to a penalty.

Please ask any queries related to the assignment on the COL100 Piazza page (https://piazza.
com/iit_delhi/fall2020/col100).

You might also like