Professional Documents
Culture Documents
cs502 Assignment 01 bc210424626
cs502 Assignment 01 bc210424626
01
Student ID:
Fall 2023
BC210424626
CS502- Fundamentals of Algorithms
Question No. 1
Solution:
Here is the simple Algorithm
1. Read the values of a, d, and n
2. Initialize a variable sum and assign 0 to it.
3. Initialize a loop from i = 0 to n-1
4. Calculate the ith term, term_i = a + i * d
5. Add term_i to the sum
6. Print term_i
4. Print the sum of the n terms
Question No. 2
Solution:
2. The middle loop j starts from 1 to 4 for each value of outerloop i. So, the number of iterations is
constant, that’s why it is O(1).
3. The innermost loop k starts from 10 to 2 and deduct 2 in each iteration of i and j. The number of
iterations is also constant here, so its time complexity is also O(1).
4. The print statement also has time complexity O(1), because it has constant number of iterations
respective of each loop.
5. The overall time complexity is calculated by product of time complexities of each loop. So,