Lec Week 2
Lec Week 2
Topics:
1. What is algorithm?
2. Are all algorithms correct?
3. What is induction and recursion?
4. How to sign up at Hackerrank and start solving practice problems there?
What is an algorithm?
In general, algorithm is a procedure to accomplish a specific task. In other words, algorithm is a way to solve
some problem.
The word algorithm comes from the name Muhammad ibn Musa al-Khwarizmi, ancient mathematician who wrote
a treatise on solving quadratic equations.
In general, anything that solves problem could be classified as algorithm. For example, here is an algorithm to
get a decent grade on CSS528:
However, in this course we will look at more formal way of looking at algorithms, i.e. well-defined problems and
well-defined steps to solve the given problem.
n = a b, a <= b n = a b >= a * a = a^2, we see that a^2 <= n, that means a <= sqrt(n)
Pseudocode:
# libraries
import math
Ans: Some algorithms could be incorrect. If this is a case, it is hard to call this procedure "algorithm".
Note: For this reason, correctness of an algorithm is very important. One should check the correctness of the
given algorithm.
Pseudo code 2:
Lesson: sometimes your algorithm might pass all you tests, but still be incorrect.
Pseudo code 3:
For instance let's prove that 2^n >= n, for all n >= 1
Problem
Pseudocode
1. if n = 1, n! = 1.
2. if n > 1, then n! = (n-1)! * n
In [8]: # implementation
def factorial(n):
if n == 1:
return 1
return factorial(n-1) * n
In [9]: print('3!: {}'.format(factorial(3)))
print('4!: {}'.format(factorial(4)))
print('5!: {}'.format(factorial(5)))
3!: 6
4!: 24
5!: 120
Hackerrank is one of algorithmic solving problems. There you could find many problems related to the topics we
will cover in this course. You are encouraged to practice solving problems related to topics that we cover for
several reasons:
In this course we will use Hackerrank for homeworks, quizzes and final exams. So please, register to Hackerrank
(as a developer, not a company) as
Please note that there are many algorithmic problem solving platforms besides Hackerrank, such as
1. Coderbyte
2. Project Euler
3. Leetcode
4. CodeWars
5. Codility
6. CodeChef
7. etc
If you find these platforms useful you could practice problem solving skills there as well. In general, these
platforms are quite silimar to each other.
In [ ]: