Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSA Lab 03

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Atif Jalal

02-235191-027
BS (IT)-3A Lab 03: Measurement of Time Complexity of an algorithm Date: 10 July, 2020

Task 1:

Source Code:
#include<iostream>
#include<math.h>
using namespace std;
void P1(int n)
{
int b=0,count=0;
for(int i=2;i<=n-1;i++)
{
if(n%i==0)
{
b=1;
}
count++;
}
if(b==1)
{
cout<<"Not a Prime Number "<<count<<endl;
}
else
{
cout<<"Prime Number "<<count<<endl;
}

}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 03: Measurement of Time Complexity of an algorithm Date: 10 July, 2020
void P2(int n)
{
int b=0,count=0;
for(int i=2;i<=sqrt((int)n);i++)
{
if(n%i==0)
{
b=1;
}
count++;
}
if(b==1)
{
cout<<"Not a Prime Number "<<count;
}
else
{
cout<<"Prime Number "<<count<<endl;
}
}
int main()
{
int a;
cout<<"Enter a Number: ";
cin>>a;
P1(a);
P2(a);
system("pause");
}

Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 03: Measurement of Time Complexity of an algorithm Date: 10 July, 2020
Task 2:
Calculate times taken by these programs for given values and conclude which algorithm is better
than other
i. n = 11
ii. n = 101
iii. n=1000003
iv. n = 10000000019
Note: Let’s assume computer takes 1 mili-second for division operation. In the worst case first
algorithm loop will exactly (n-2) times or (n-2) division and in second algorithm (√n – 1) times

Output:

You might also like