Assignment3
Assignment3
Assignment 2
Name: _______________________________
Section: ________A23_____________________
CLO 3 The students will be able to apply fundamental concepts of loops and time C3 PLO 2
complexity.
Instructions:
1. Print this assignment and solve on it.
2. Write your answer showing all steps required to perform the task.
3. Assignment must be solved on A4 sheets or Assignment sheets only. Violation will result to deduction
of 10 mark from the scored marks. Submissions will be made either via portal or in the class.
4. Due Date for Assignment is: 09 January, 2025.
5. Late submission will result in 10% deduction in marks.
6. No request for late submissions will be considered after two working days of the deadline.
7. No request to review assignment will be considered after 2 working days of the review in class.
SOME USEFUL INFORMATIONS
(you must understand these bounds first. They are the crux of Time bounding)
Note: Print this assignment and solve on it. Make table for each loop.
Arithmetic Sequence Size
1, 2, 3, 4, 5, 6, .... , N ≤ O(N)
1, 3, 5, 7, .... , N ≤ N/2 i.e O(N)
1, 4, 7, 10, .... , N ≤ N/3 i.e. O(N)
1, 1+k, 1+2k, 1+3k, 1+4k, 1+5k, .... , N ≤ N/k i.e. O(N) if k is a constant
____________________________________________________________
𝑁(𝑁+1)
Arithmetic Series Applications of 1+2+3+4+...+N =
2
1+2+3+4+5+6+ ....N-3+ N-2+ N-1+ N ≤ O(N2)
1+2+3+4+5+6+ ....(N/2-3)+ (N/2-2)+ (N/2-1)+ N/2 ≤ O(N2)
1+2+3+4+5+6+ ....(N/3-3)+ (N/3-2)+ (N/3-1)+ N/3 ≤ O(N2)
1+2+3+4+5+6+ .... +√𝑁 <= O((√𝑁)2) ≤ O(N)
1+2+3+4+5+6+ .... + N2 ≤ O(N4)
1+2+3+4+5+6+ .... + N3 ≤ O(N6)
1+2+3+4+5+6+ .... +Nk <= O(NkxNk)
1+22+32+42+52+62+ .... + N2 ≤ O(N3)
1+23+33+43+53+63+ .... + N3 ≤ O(N4)
1k+2k+3k+4k+5k+6k+ .... +Nk <= O(Nk+1)
____________________________________________________________
Geometric Sequence Size
N, N/2, N/4, N/8, N/24, N/25 , N/26, …8, 4 , 2 , 1 <= log 2 𝑁
N, N/5, N/25, N/125, N/54, N/55 , N/56, …, 53, 52, 5 , 1 <= log 5 𝑁
O(log N)
N, N/k, N/k2, N/k3, N/k4, N/k5 , N/k6, … , k3, k2, k, 1 <= 𝐥𝐨𝐠 𝐤 𝑵
____________________________________________________________
Application
____________________________________________________________
GEOMETRIC SERIES
N < 1+2+4+8+16+32+... +N/4+N/2+N < 2N
N < 1+3+9+34+35+... +N/32+N/3+N < 2N
N < 1+5+52+53+54+55+... +N/52+N/5+N < 2N
O(N)
TIME COMPLEXITY
1) What is the algorithm’s complexity of the following 2) What is the algorithm’s complexity of the following
piece of code piece of code
int Sum=0;
int Sum=0; for(int i=0; i<N; i++)
for(int i=0; i<N; i++) Sum++;
for(int j=0; j<N; j++) for(int j=0; j<N; j++)
Sum++; Sum++;
Total: Total:
3) 4)
Sum++; Sum++;
𝑻𝒐𝒕𝒂𝒍: 𝑻𝒐𝒕𝒂𝒍:
5) 6)
int Sum=0;
for(int i=0; i<N; i++)
int Sum=0; Sum++;
for(int i=0; i<N; i++) for(int j=0; j<N; j++)
for(int j=0; j<N; j++)
for(int k=0; k<N; k++) Sum++;
for(int k=0; k<N; k++)
Sum++;
Sum++;
for(int m=0; m<N; m++)
for(int i=0; i<N; i++) Sum++;
for(int j=0; j<N; j++) for(int n=0; n<N; n++)
for(int k=0; k<N; k++)
Sum++;
Sum++; for(int p=0; p<N; p++)
Sum++;
Total = Total =
7) 8)
Sum++;
Total =
Total =
9) 10)
int Sum=0;
for(int i=1; i<N; i*=2) ) int Sum=0;
Sum++; for(int i=1; i<N; i*=2)
for(int j=1; j<N; j*=2) for(int j=1; j<N; j*=2)
Sum++; Sum++;
𝑻𝒐𝒕𝒂𝒍 =
𝑻𝒐𝒕𝒂𝒍 =
11) 12)
for(int i=1; i<=N*N; i+=2) for(int i=1; i<=N*N; i+=2)
for(int j=1; j<N*N; j*=2) Sum++;
Sum++;
for(int j=1; j<N*N; j*=2)
Sum++;
𝑻𝒐𝒕𝒂𝒍 = 𝑻𝒐𝒕𝒂𝒍 =
13) 14)
for(int i=1; i<=N*N; i*=2) for(int i=1; i<=N*N; i*=2)
for(int j=1; j<N*N; j*=2) Sum++;
Sum++;
for(int j=1; j<N*N; j*=2)
Sum++;
𝑻𝒐𝒕𝒂𝒍 =
𝑻𝒐𝒕𝒂𝒍 =
15) 16)
int Sum=0; int Sum=0;
for(int i=1; i<=N; i*=2) for(int i=1; i<=N; i*=2)
for(int j=1; j<=N; j*=2) Sum++;
for(int k=1; k<=N; k*=2) for(int j=1; j<=N; j*=2)
Sum++; Sum++;
for(int k=1; k<=N; k*=2)
Sum++;
𝑻𝒐𝒕𝒂𝒍 =
Total=
17) 18)
int sum, i, j; BE CAREFUL GEOMETRIC SERIES
int sum,i,j;
sum = 0;
sum = 0;
for (i=1; i<n; i=i*2)
for (i=1; i<n; i=i*2)
{
for (j=0; j<n;++j) {
for (j=0; j <i ; ++j)
{
sum++; {
sum++;
}
}
} 𝑻𝒐𝒕𝒂𝒍 =
} 𝑼𝒔𝒊𝒏𝒈 𝒊𝒏𝒑𝒖𝒕 𝒕𝒂𝒃𝒍𝒆 𝒇𝒐𝒓 𝒊 𝒂𝒏𝒅 𝒋, 𝒘𝒆 𝒘𝒊𝒍𝒍 𝒈𝒆𝒕,
(𝟏 + 𝟐 + 𝟒 + ⋯ + 𝑵) ≤ 𝟐𝑵 =
19) 20)
BE CAREFUL GEOMETRIC SERIES int sum,i,j;
int sum,i,j;
sum = 0;
sum = 0;
for (i=1; i<n; i=i*4)
for (i=1; i<n; i=i*5)
{
{ for (j=0 ; j<n ; j+=3)
for (j=0; j<i; j+=2)
{
{ sum++;
sum++;
}
}
}
} 𝑼𝒔𝒊𝒏𝒈 𝒊𝒏𝒑𝒖𝒕 𝒕𝒂𝒃𝒍𝒆 𝒇𝒐𝒓 𝒊 𝒂𝒏𝒅 𝒋, 𝒘𝒆 𝒘𝒊𝒍𝒍 𝒈𝒆𝒕,
𝑶(𝒏)=
𝑻𝒐𝒕𝒂𝒍 =
21 22
What will be the output (the value of Sum) of the What will be the output(the value of Sum) of the
program asymptotically in BIG-O notation, I am not program asymptotically in BIG-O notation:
asking here the complexity of loop rather the
asymptotic bound on the value of Sum: int Sum = 0;
for(int i=1; i<=n; i*=2)
int Sum = 0; {
for(int i=1; i<=n; i+=1) 𝑶(𝒏) Sum+=i;
{ }
Sum+=i; cout<<Sum<<endl;
}
cout<<Sum<<endl;
𝑺𝒖𝒎 =
𝑺𝒖𝒎 =
23) 24)
What is the time complexity of the algorithm: What is the time complexity of the algorithm:
int Sum = 0; int Sum = 0;
for(int i=1; i<=n; i+=1) for(int i=1; i<n; i*=2)
{ {
for(int j=1; j<=i; j++) for(int j=1; j<=i; j++)
{ {
Sum++; Sum++;
} }
} }
cout<<Sum<<endl; cout<<Sum<<endl;
𝑼𝒔𝒊𝒏𝒈 𝒊𝒏𝒑𝒖𝒕 𝒕𝒂𝒃𝒍𝒆, 𝟏 + 𝟐 + 𝟑 + ⋯ + 𝒏 = 𝑼𝒔𝒊𝒏𝒈 𝒊𝒏𝒑𝒖𝒕 𝒕𝒂𝒃𝒍𝒆 𝒇𝒐𝒓 𝒊 𝒂𝒏𝒅 𝒋, 𝒘𝒆 𝒘𝒊𝒍𝒍 𝒈𝒆𝒕,
(𝟏 + 𝟐 + 𝟒 + ⋯ + 𝑵) ≤ 𝟐𝑵 =
25) 26)
What is the time complexity of the algorithm: What is the time complexity of the algorithm:
void f1(int n) void f1(int n)
{ {
for(int j=0; j*j<=n*n; j++) for(int j=1; j*j<=n; j*=2)
K++; K++;
return K; return K;
} }
int main() int main()
{ {
int Sum = 0; int Sum = 0;
int n; int n;
cin>>n; cin>>n;
for(int i=1; i<=f1(n); i+=1)
{
for(int i=1; i<=f1(n); i+=1) for(int j=1; j<=i; j++)
{ {
for(int j=1; j<=i; j++) Sum++;
{ }
Sum++; }
} cout<<Sum<<endl;
} }
cout<<Sum<<endl;
} 𝑼𝒔𝒊𝒏𝒈 𝒊𝒏𝒑𝒖𝒕 𝒕𝒂𝒃𝒍𝒆,
What is the time complexity of the algorithm: What is the time complexity of the algorithm:
void f1(int n)
{ void f1(int n)
for(int j=1; j*j<=n; j++) {
for(int j=0; j*j<=n; j++)
K++;
K++;
return K*K;
} return K;
int main() }
{ int main()
int Sum = 0; {
int n; int Sum = 0;
cin>>n; int n;
cin>>n;
int Terminator = f1(n); int Terminator = f1(n);
for(int i=1; i<= Terminator; i+=1) for(int i=1; i<=Terminator; i+=1)
{ {
for(int j=1; j<=i; j++) for(int j=1; j<=i; j++)
{ {
Sum++; Sum++;
} }
} }
cout<<Sum<<endl; cout<<Sum<<endl;
} }
29) 30)
{ {
{ {
} }
} }
31) 32)
for (i=1; i<=n*n; ++i) for (i=1; i<=n*n*n; ++i)
{ {
cout << i; cout << i;
Sum=0; Sum=0;
{ {
Sum++; Sum++;
} }
} }
{ {
cout << i; cout << i;
Sum=0; Sum=0;
{ {
Sum++; Sum++;
} }
for (k=1; k<=n; k++)
cout << Sum;
{
}
Sum++;
cout << i;
𝑼𝒔𝒊𝒏𝒈 𝒈𝒆𝒐𝒎𝒆𝒕𝒓𝒊𝒄 𝒔𝒆𝒓𝒊𝒆𝒔, 𝑻𝒐𝒕𝒂𝒍 =
}
𝑻𝒐𝒕𝒂𝒍 =
35) 36)
for (i=1; i<=n*n*n; i*=2) for (i=1; i<=n*n*n; i*=2)
{ {
cout << i; cout << i;
Sum=0; Sum=0;
{ {
Sum++; Sum++;
{ {
Sum++; Sum++;
} }
} }
37-38 37
for (i=0; i<n; i=i+3)
for (int i=1; i <= n ; i = i * 2)
{ {
} {
cout << j;
sum++
sum++
for (int i=1; i <= n ; i = i * 2)
for ( j = 1 ; j <= i ; j = j * 2) }
cout<<”*”;
cout << sum;
𝑻𝒐𝒕𝒂𝒍 =
39) 40)
for (int i=1; i <= n ; i = i * 2) for (i=0; i<n; i=i+3)
{
{
for ( j = 1 ; j <= i ; j = j * 2)
cout << i;
{
for (j=1; j<n; j=j*3)
cout<<”*”;
{
}
sum++
}
}
cout << j;
𝑻𝒐𝒕𝒂𝒍 = sum++
𝑻𝒐𝒕𝒂𝒍 =
41) 42)
Complexity of primeNumber function. Complexity of primeNumber function.
int sqrt(int N) int sqrt(int N)
{ {
int d; int d;
for(d=0; d*d<=N; d++) { } for(d=0; d*d<=N; d++){ }
return d-1; return d-1;
} }
bool primeNumber(int n) bool primeNumber(int n)
{ {
{ if (n%d==0)
return false; }
} return true;