762 Daalab2
762 Daalab2
CODE -
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int i, count = 0;
if(number%i == 0)
count=1;
}i
f(number == 1)
count = 1;
return count;
}
int Algo2(int number)
int i, count = 0;
if(number%i == 0)
count=1;
break;
if(number == 1)
count = 1;
return count;
if (number <= 2)
return (number == 2) ? 1: 0;
if (number % i == 0)
return 0;
if (i * i > number)
return 1;
int main()
int number,t ;
clock_t start,end;
double cputime;
scanf("%d",&number);
printf("\n");
//Algo1
start=clock();
t=Algo1(number);
if( t == 0)
else
end=clock();
cputime=(double)(end-start)/CLOCKS_PER_SEC;
//Algo2
start=clock();
t=Algo2(number);
if( t == 0)
end=clock();
cputime=((double)(end-start))/CLOCKS_PER_SEC;
//Algo3
start=clock();
if (Algo3(number,2))
else
end=clock();
cputime=((double)(end-start))/CLOCKS_PER_SEC;
return 0;
}
OUTPUT –
GRAPH -
2.2) Write a program to find out GCD (greatest common divisor) using the
following three algorithms.
a) Euclid’s algorithm
c) Middle school procedure which makes use of common prime factors. For
finding list of primes implement sieve of Eratosthenes algorithm.
Write a program to find out which algorithm is faster for the following data.
Estimate how many times it will be faster than the other two by
step/frequency count method in each case.
i. Find GCD of two numbers when both are very large i.e. GCD (31415,
14142) by applying each of the above algorithms.
ii. Find GCD of two numbers when one of them can be very large i.e. GCD
(56,32566) or GCD(34218, 56) by applying each of the above algorithms.
iii. Find GCD of two numbers when both are very small i.e. GCD(12,15) by
applying each of the above algorithms.
iv. Find GCD of two numbers when both are same i.e. GCD(31415, 31415)
or GCD(12, 12) by applying each of the above algorithms.
Write the above data in the following format and decide which algorithm is
faster for the particular data.
CODE –
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
//Euclid algorithm
if (y == 0)
return x;
else if (x >= y && y > 0)
int t;
if (m < n)
t = m;
else
t = n;
while (t != 1)
return t;
t = t - 1;
typedef struct{
int size;
} FACTORIZATION;
void FindFactorization(int x, FACTORIZATION* factorization)
int i, j = 1;
int n = x, c = 0;
int k = 1;
factorization->factor[0] = 1;
factorization->exponent[0] = 1;
c = 0;
while (n % i == 0) {
c++;
n = n / i;
}i
f (c > 0) {
factorization->exponent[k] = c;
factorization->factor[k] = i;
k++;
} factorization->size =
1;
}i
nt MSP(int m, int n)
{
FindFactorization(m, &mFactorization);
FindFactorization(n, &nFactorization);
int min;
i = 1;
j = 1;
i++;
j++;
else{
nFactorization.exponent[j] : mFactorization.exponent[i];
x = x * mFactorization.factor[i] * min;
i++;
j++;
return x;
int main()
{i
nt a,b;
clock_t start,end;
double total_cputime;
scanf("%d",&a);
scanf("%d",&b);
//Euclid
start=clock();
int gcd1=Euclid(a,b);
printf("\nGcd:%d\n",gcd1);
end=clock();
total_cputime=(double)(end-start);
total_cputime=((double)(end-start))/CLOCKS_PER_SEC;
printf("\n\n");
int gcd2=CIC(a,b);
printf("\nGcd:%d\n",gcd2);
end=clock();
total_cputime=(double)(end-start);
total_cputime=((double)(end-start))/CLOCKS_PER_SEC;
printf("\n\n");
start=clock();
int gcd3=MSP(a,b);
printf("\nGcd:%d\n",gcd3);
end=clock();
total_cputime=(double)(end-start);
total_cputime=((double)(end-start))/CLOCKS_PER_SEC;
printf("\n\n");
return 0;
OUTPUT –
TABLE –
HOME EXERCISE -
CODE –
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int ch,n=5000;
clock_t start,end;
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
void decresing(int arr[],int n){
int a;
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
insertionSort(arr, n);
start=clock();
insertionSort(arr, n);
end=clock();
return (double)(end-start)/CLOCKS_PER_SEC;
decresing(arr, n);
start=clock();
insertionSort(arr, n);
end=clock();
return (double)(end-start)/CLOCKS_PER_SEC;
start=clock();
insertionSort(arr, n);
end=clock();
return (double)(end-start)/CLOCKS_PER_SEC;
}i
nt main()
int arr[n],arr1[n];
double totalcpu;
while(1){
scanf("%d",&ch);
switch(ch){
case 0:{
exit(0);
case 1:{
srand(time(NULL));
for(int i=0;i<n;i++)
arr[i]=rand()%n;
break;
case 2:{
for(int i=0;i<n;i++)
printf("%d\n",arr[i]);
break;
case 3:{
insertionSort(arr, n);
for(int i=0;i<n;i++)
printf("%d\n",arr[i]);
break;
case 4:{
decresing(arr, n);
for(int i=0;i<n;i++)
printf("%d\n",arr[i]);
break;
}
case 5:{
totalcpu=timerand(arr);
break;
case 6:{
totalcpu=timeascen(arr);
break;
case 7:{
totalcpu=timedescen(arr);
break;
case 8:{
int
inputcase[10]={5000,10000,15000,20000,25000,30000,35000,4
0000,45000,
50000};
for(int i=0;i<10;i++){
printf("%d ",inputcase[i]);
//Random data
totalcpu=timerand(arr1);
printf("%f ",totalcpu);
//Descending data
totalcpu=timedescen(arr);
printf("%f ",totalcpu);
//Ascending data
totalcpu=timeascen(arr);
printf("%f ",totalcpu);
printf("\n");
break;
}}
return 0;
}
OUTPUT –
GRAPH -
TABLE -
Sl Value of n Time Complexity Time Complexity Time Complexity
No. (Data in Ascending) (Data in descending) (Random Data)
1 5000 0.000024 0.041080 0.021093