Q1 Q2
Solution:
Solution:
The initialization, int i=1 will execute one time in
There are three loops: I,j, and k. first initialization; the outer while loop executes
upto N -1 times
The k loop will execute if the condition is satisfied. Int j = N executes one time; then the inner while
loop executes starts executing N times until 1,
In this case, k will always be executed since for all since j>0 in the condition; also decrease by j=j/2 ,
j%i in the modulus operation, the result is 0. then lastly the I is incremented by a factor of two,
Thus: i.e i=2*i.
I: 1 2 3 4 ….. n Hence, the time complexity is measured as,
J: 1 4 9 16 ….. (𝑛)2
1
NILE UNIVERSITY OF NIGERIA
DEPARTMENT OF COMPUTER SCIENCE
Course: Analysis of Algorithm
Assignment 1
Q4
The series given for j results to,
1 1 1 1
𝑁 (1 + 2 + 4 + 8 + ⋯ + 𝑁) = 2𝑁 as the sum to
infinitesimal converges to 2 as N gets larger .
How? The is sum of converging series, because as
1 𝑁
Solution:
1 (1−( ) )
2
N gets larger then 𝑁
→0 𝑆= 1 =
1−
2 I: 1 2 3 4 ….. n
1
2 (1 − 2𝑁
) = 2(1 − 0) = 2
J: 1 2 4 8 …… k term
Therefore, total time complexity is The j loop is a geometric sequence or ratio 2;
So the sum is,
𝑙𝑜𝑔2 𝑁 ∗ 2𝑁 = 𝑂(𝑁𝑙𝑜𝑔2 𝑁)
𝑎(𝑟 𝑛 − 1) 1. (2𝑘 − 1)
Q3 = ≅ 2𝑘
𝑟−1 2−1
Then 𝑘 = 𝑙𝑜𝑔2 𝑁
I: 1 2 3 4 ….. n Solution
J: 1 3 6 10 ….. k term 𝑁
I: 1 2 3 4 ….. 3
Q7
Same as Q 1
2
NILE UNIVERSITY OF NIGERIA
DEPARTMENT OF COMPUTER SCIENCE
Course: Analysis of Algorithm
Assignment 1