Week2 Algorithmic Warmup
Week2 Algorithmic Warmup
Algorithmic Warm-up
Revision: January 2, 2020
Introduction
Welcome to your second programming assignment of the Algorithmic Toolbox at Coursera! It consists of eight
programming challenges. Three of them require you just to implement carefully the algorithms covered in
the lectures. The remaining challenges will require you to first design an algorithm and then to implement it.
For all the challenges, we provide starter solutions in C++, Java, and Python3. These solutions implement
straightforward algorithms that usually work only for small inputs. To verify this, you may want to submit
these solutions to the grader. This will usually give you a “time limit exceeded” message for Python
starter files and either “time limit exceeded” or “wrong answer” message for C++ and Java solutions (the
reason for wrong answer being an integer overflow issue). Your goal is to replace a naive algorithm with
an efficient one. In particular, you may want to use the naive implementation for stress testing your efficient
implementation.
In this programming assignment, the grader will show you the input data if your solution fails on any
of the tests. This is done to help you to get used to the algorithmic problems in general and get some
experience debugging your programs while knowing exactly on which tests they fail. However, for all the
following programming assignments, the grader will show the input data only in case your solution fails on
one of the first few tests.
Learning Outcomes
Upon completing this programming assignment you will be able to:
1. See the huge difference between a slow algorithm and a fast one.
2. Play with examples where knowing something interesting about a problem helps to design an algorithm
that is much faster than a naive one.
3. Implement solutions that work much more faster than straightforward solutions for the following pro-
gramming challenges:
(a) compute a small Fibonacci number;
(b) compute the last digit of a large Fibonacci number;
(c) compute a huge Fibonacci number modulo 𝑚;
(d) compute the last digit of a sum of Fibonacci numbers;
(e) compute the last digit of a partial sum of Fibonacci numbers;
(f) compute the greatest common divisor of two integers;
(g) compute the least common multiple of two integers.
4. Implement the algorithms covered in the lectures, design new algorithms.
5. Practice implementing, testing, and debugging your solution. In particular, you will find out how in
practice, when you implement an algorithm, you bump into unexpected questions and problems not
covered by the general description of the algorithm. You will also check your understanding of the
algorithm itself and most probably see that there are some aspects you did not think of before you
had to actually implement it. You will overcome all those complexities, implement the algorithms, test
them, debug, and submit to the system.
1
Passing Criteria: 4 out of 8
Passing this programming assignment requires passing at least 4 out of 8 programming challenges from this
assignment. In turn, passing a programming challenge requires implementing a solution that passes all the
tests for this problem in the grader and does so under the time and memory limits specified in the problem
statement.
Contents
1 Fibonacci Number 3
9 Appendix 12
9.1 Compiler Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
9.2 Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2
1 Fibonacci Number
Problem Introduction
Recall the definition of Fibonacci sequence: 𝐹0 = 0, 𝐹1 = 1, and 𝐹𝑖 = 𝐹𝑖−1 + 𝐹𝑖−2 for
𝑖 ≥ 2. Your goal in this problem is to implement an efficient algorithm for computing
Fibonacci numbers. The starter files for this problem contain an implementation of
the following naive recursive algorithm for computing Fibonacci numbers in C++,
Java, and Python3:
Fibonacci(𝑛):
if 𝑛 ≤ 1:
return 𝑛
return Fibonacci(𝑛 − 1) + Fibonacci(𝑛 − 2)
Try compiling and running a starter solution on your machine. You will see that
computing, say, 𝐹40 already takes noticeable time.
Another way to appreciate the dramatic difference between an exponential time algo-
rithm and a polynomial time algorithm is to use the following visualization by David
Galles: http://www.cs.usfca.edu/~galles/visualization/DPFib.html. Try com-
puting 𝐹20 by a recursive algorithm by entering “20” and pressing the “Fibonacci Re-
cursive” button. You will see an endless number of recursive calls. Now, press “Skip
Forward” to stop the current algorithm and call the iterative algorithm by pressing
“Fibonacci Table”. This will compute 𝐹20 very quickly. (Note that the visualization
uses a slightly different definition of Fibonacci numbers: 𝐹0 = 1 instead of 𝐹0 = 0.
This of course has almost no influence on the running time.)
Problem Description
Task. Given an integer 𝑛, find the 𝑛th Fibonacci number 𝐹𝑛 .
Input Format. The input consists of a single integer 𝑛.
Constraints. 0 ≤ 𝑛 ≤ 45.
Output Format. Output 𝐹𝑛 .
Sample 1.
Input:
10
Output:
55
𝐹10 = 55.
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
3
2 Last Digit of a Large Fibonacci Number
Problem Introduction
Your goal in this problem is to find the last digit of 𝑛-th Fibonacci number. Recall that Fibonacci numbers
grow exponentially fast. For example,
𝐹200 = 280 571 172 992 510 140 037 611 932 413 038 677 189 525 .
Problem Description
Task. Given an integer 𝑛, find the last digit of the 𝑛th Fibonacci number 𝐹𝑛 (that is, 𝐹𝑛 mod 10).
Input Format. The input consists of a single integer 𝑛.
Constraints. 0 ≤ 𝑛 ≤ 107 .
Sample 1.
Input:
3
Output:
2
𝐹3 = 2.
Sample 2.
Input:
331
Output:
9
𝐹331 = 668 996 615 388 005 031 531 000 081 241 745 415 306 766 517 246 774 551 964 595 292 186 469.
4
Sample 3.
Input:
327305
Output:
5
𝐹327305 does not fit into one line of this pdf, but its last digit is equal to 5.
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
5
3 Greatest Common Divisor
Problem Introduction
The greatest common divisor GCD(𝑎, 𝑏) of two non-negative integers 𝑎 and 𝑏
GCD(1344, 217)
(which are not both equal to 0) is the greatest integer 𝑑 that divides both 𝑎 and 𝑏.
Your goal in this problem is to implement the Euclidean algorithm for computing = GCD(217, 42)
the greatest common divisor. = GCD(42, 7)
Efficient algorithm for computing the greatest common divisor is an important = GCD(7, 0)
basic primitive of commonly used cryptographic algorithms like RSA.
=7
Problem Description
Task. Given two integers 𝑎 and 𝑏, find their greatest common divisor.
Input Format. The two integers 𝑎, 𝑏 are given in the same line separated by space.
Constraints. 1 ≤ 𝑎, 𝑏 ≤ 2 · 109 .
Output Format. Output GCD(𝑎, 𝑏).
Sample 1.
Input:
18 35
Output:
1
Sample 2.
Input:
28851538 1183019
Output:
17657
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
6
4 Least Common Multiple
Problem Introduction
The least common multiple of two positive integers 𝑎 and 𝑏 is the least positive
integer 𝑚 that is divisible by both 𝑎 and 𝑏.
Problem Description
Task. Given two integers 𝑎 and 𝑏, find their least common multiple.
Input Format. The two integers 𝑎 and 𝑏 are given in the same line separated by space.
Constraints. 1 ≤ 𝑎, 𝑏 ≤ 107 .
Output Format. Output the least common multiple of 𝑎 and 𝑏.
Sample 1.
Input:
68
Output:
24
Among all the positive integers that are divisible by both 6 and 8 (e.g., 48, 480, 24), 24 is the smallest
one.
Sample 2.
Input:
761457 614573
Output:
467970912861
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
7
5 Fibonacci Number Again
Problem Introduction
In this problem, your goal is to compute 𝐹𝑛 modulo 𝑚, where 𝑛 may be really huge: up to 1014 . For such
values of 𝑛, an algorithm looping for 𝑛 iterations will not fit into one second for sure. Therefore we need to
avoid such a loop.
To get an idea how to solve this problem without going through all 𝐹𝑖 for 𝑖 from 0 to 𝑛, let’s see what
happens when 𝑚 is small — say, 𝑚 = 2 or 𝑚 = 3.
𝑖 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
𝐹𝑖 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
𝐹𝑖 mod 2 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
𝐹𝑖 mod 3 0 1 1 2 0 2 2 1 0 1 1 2 0 2 2 1
Take a detailed look at this table. Do you see? Both these sequences are periodic! For 𝑚 = 2, the period
is 011 and has length 3, while for 𝑚 = 3 the period is 01120221 and has length 8. Therefore, to compute,
say, 𝐹2015 mod 3 we just need to find the remainder of 2015 when divided by 8. Since 2015 = 251 · 8 + 7, we
conclude that 𝐹2015 mod 3 = 𝐹7 mod 3 = 1.
This is true in general: for any integer 𝑚 ≥ 2, the sequence 𝐹𝑛 mod 𝑚 is periodic. The period always
starts with 01 and is known as Pisano period.
Problem Description
Task. Given two integers 𝑛 and 𝑚, output 𝐹𝑛 mod 𝑚 (that is, the remainder of 𝐹𝑛 when divided by 𝑚).
Input Format. The input consists of two integers 𝑛 and 𝑚 given on the same line (separated by a space).
Sample 1.
Input:
239 1000
Output:
161
𝐹239 mod 1 000 = 39 679 027 332 006 820 581 608 740 953 902 289 877 834 488 152 161 (mod 1 000) = 161.
Sample 2.
Input:
2816213588 239
Output:
151
𝐹2 816 213 588 does not fit into one page of this file, but 𝐹2 816 213 588 mod 239 = 151.
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
8
6 Last Digit of the Sum of Fibonacci Numbers
Problem Introduction
The goal in this problem is to find the last digit of a sum of the first 𝑛 Fibonacci numbers.
Problem Description
Task. Given an integer 𝑛, find the last digit of the sum 𝐹0 + 𝐹1 + · · · + 𝐹𝑛 .
Input Format. The input consists of a single integer 𝑛.
Constraints. 0 ≤ 𝑛 ≤ 1014 .
Output Format. Output the last digit of 𝐹0 + 𝐹1 + · · · + 𝐹𝑛 .
Sample 1.
Input:
3
Output:
4
𝐹0 + 𝐹1 + 𝐹2 + 𝐹3 = 0 + 1 + 1 + 2 = 4.
Sample 2.
Input:
100
Output:
5
The sum is equal to 927 372 692 193 078 999 175, the last digit is 5.
What To Do
Instead of computing this sum in a loop, try to come up with a formula for 𝐹0 + 𝐹1 + 𝐹2 + · · · + 𝐹𝑛 . For
this, play with small values of 𝑛. Then, use a solution for the previous problem.
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
Solution
A detailed solution (with Python code) for this challenge is covered in the companion MOOCBook. We
strongly encourage you to do your best to solve the challenge yourself before looking into the book! There
are at least three good reasons for this.
∙ By solving this challenge, you practice solving algorithmic problems similar to those given at technical
interviews.
∙ The satisfaction and self confidence that you get when passing the grader is priceless =)
∙ Even if you fail to pass the grader yourself, the time will not be lost as you will better understand the
solution from the book and better appreciate the beauty of the underlying ideas.
9
7 Last Digit of the Sum of Fibonacci Numbers Again
Problem Introduction
Now, we would like to find the last digit of a partial sum of Fibonacci numbers: 𝐹𝑚 + 𝐹𝑚+1 + · · · + 𝐹𝑛 .
Problem Description
Task. Given two non-negative integers 𝑚 and 𝑛, where 𝑚 ≤ 𝑛, find the last digit of the sum 𝐹𝑚 + 𝐹𝑚+1 +
· · · + 𝐹𝑛 .
Input Format. The input consists of two non-negative integers 𝑚 and 𝑛 separated by a space.
Constraints. 0 ≤ 𝑚 ≤ 𝑛 ≤ 1014 .
Output Format. Output the last digit of 𝐹𝑚 + 𝐹𝑚+1 + · · · + 𝐹𝑛 .
Sample 1.
Input:
37
Output:
1
𝐹3 + 𝐹4 + 𝐹5 + 𝐹6 + 𝐹7 = 2 + 3 + 5 + 8 + 13 = 31.
Sample 2.
Input:
10 10
Output:
5
𝐹10 = 55.
Sample 3.
Input:
10 200
Output:
2
𝐹10 + 𝐹11 + · · · + 𝐹200 = 734 544 867 157 818 093 234 908 902 110 449 296 423 262
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
10
8 Last Digit of the Sum of Squares of Fibonacci Numbers
Problem Description
Task. Compute the last digit of 𝐹02 + 𝐹12 + · · · + 𝐹𝑛2 .
Input Format. Integer 𝑛.
Constraints. 0 ≤ 𝑛 ≤ 1014 .
Output Format. The last digit of 𝐹02 + 𝐹12 + · · · + 𝐹𝑛2 .
Sample 1.
Input:
7
Output:
3
Sample 2.
Input:
73
Output:
1
𝐹02 + · · · + 𝐹73
2
= 1 052 478 208 141 359 608 061 842 155 201.
Sample 3.
Input:
1234567890
Output:
0
What To Do
Since the brute force search algorithm for this problem is too slow (𝑛 may be as large as 1018 ), we need to come
up with a simple formula for 𝐹02 + 𝐹12 + · · · + 𝐹𝑛2 . The figure below represents the sum 𝐹12 + 𝐹22 + 𝐹32 + 𝐹42 + 𝐹52
as the area of a rectangle with vertical side 𝐹5 = 5 and horizontal side 𝐹5 + 𝐹4 = 3 + 5 = 𝐹6 .
3
5
1
2
1
Need Help?
Ask a question or see the questions asked by other learners at this forum thread.
11
9 Appendix
9.1 Compiler Flags
C (gcc 5.2.1). File extensions: .c. Flags:
gcc - pipe - O2 - std = c11 < filename > - lm
If your C/C++ compiler does not recognize -std=c++14 flag, try replacing it with -std=c++0x flag
or compiling without this flag at all (all starter solutions can be compiled without it). On Linux
and MacOS, you most probably have the required compiler. On Windows, you may use your favorite
compiler or install, e.g., cygwin.
Python 2 (CPython 2.7). File extensions: .py2 or .py (a file ending in .py needs to have a first line which
is a comment containing “python2”). No flags:
python2
Python 3 (CPython 3.4). File extensions: .py3 or .py (a file ending in .py needs to have a first line which
is a comment containing “python3”). No flags:
python3
12
Rust (Rust 1.28.0). File extensions: .rs.
rustc
∙ Good job! Hurrah! Your solution passed, and you get a point!
∙ Wrong answer. Your solution outputs incorrect answer for some test case. Check that you consider
all the cases correctly, avoid integer overflow, output the required white spaces, output the floating
point numbers with the required precision, don’t output anything in addition to what you are asked
to output in the output specification of the problem statement.
∙ Time limit exceeded. Your solution worked longer than the allowed time limit for some test case.
Check again the running time of your implementation. Test your program locally on the test of max-
imum size specified in the problem statement and check how long it works. Check that your program
doesn’t wait for some input from the user which makes it to wait forever.
∙ Memory limit exceeded. Your solution used more than the allowed memory limit for some test case.
Estimate the amount of memory that your program is going to use in the worst case and check that it
does not exceed the memory limit. Check that your data structures fit into the memory limit. Check
that you don’t create large arrays or lists or vectors consisting of empty arrays or empty strings, since
those in some cases still eat up memory. Test your program locally on the tests of maximum size
specified in the problem statement and look at its memory consumption in the system.
∙ Cannot check answer. Perhaps the output format is wrong. This happens when you output
something different than expected. For example, when you are required to output either “Yes” or
“No”, but instead output 1 or 0. Or your program has empty output. Or your program outputs not
only the correct answer, but also some additional information (please follow the exact output format
specified in the problem statement). Maybe your program doesn’t output anything, because it crashes.
13
∙ Unknown signal 6 (or 7, or 8, or 11, or some other). This happens when your program
crashes. It can be because of a division by zero, accessing memory outside of the array bounds, using
uninitialized variables, overly deep recursion that triggers a stack overflow, sorting with a contradictory
comparator, removing elements from an empty data structure, trying to allocate too much memory,
and many other reasons. Look at your code and think about all those possibilities. Make sure that you
use the same compiler and the same compiler flags as we do.
∙ Internal error: exception... Most probably, you submitted a compiled program instead of
a source code.
∙ Grading failed. Something wrong happened with the system. Report this through Coursera or edX
Help Center.
14