-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.c
134 lines (120 loc) · 2.87 KB
/
gcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define x 10
#define y 200
#define incre 10
// To analyze all algorithms in single execution
// plot the graphs of different algorithms separately
void main()
{
int i,j,k,m,n,r,itn=1,tm,tn,min,E_Maxcount, E_Mincount,E_count, ME_Maxcount, ME_Mincount,ME_count, temp, CI_Maxcount,CI_Mincount,CI_count;
FILE *fp1,*fp2, *fp3, *fp4, *fp5,*fp6,*fp7;
// system("rm GCD_data.txt");
for(i=x;i<=y;i+=incre)
{
E_Maxcount = 0;
E_Mincount= 999;
ME_Maxcount = 0;
ME_Mincount= 999;
CI_Maxcount = 0;
CI_Mincount= 999;
for(j=2;j<=i;j++)
{
for(k=2;k<=i;k++)
{
m = j;
n = k;
tm = m;
tn = n;
// Euclid Algorithm
{
E_count=0 ;
while(n!=0)
{
++E_count;
r = m%n;
m = n;
n = r;
}
if(E_count> E_Maxcount)
E_Maxcount = E_count;
if(E_count < E_Mincount)
E_Mincount = E_count;
}
// end of Euclid
// Modified Euclid
{
m=tm; n = tn;
ME_count= 0;
while(n!=0)
{
if(n>m)
{
temp=m;
m=n;
n=temp;
}
++ME_count;
m= m- n;
}
if(ME_count> ME_Maxcount)
ME_Maxcount =ME_count;
if(ME_count< ME_Mincount)
ME_Mincount= ME_count;
}
// end of modified euclid
// consecutive integer checking method
{
m=tm;
n=tm;
min=m;
CI_count=0;
if(min>n)
min=n;
while(min!=0)
{
++CI_count;
if(m%min==0)
{
++CI_count;
if(n%min==0)
break;
//else
//min--;
}
//else
min--;
}
}
if(CI_count> CI_Maxcount)
CI_Maxcount =CI_count;
if(CI_count< CI_Mincount)
CI_Mincount= CI_count;
//end of consecutive integer method
// fp1=fopen("GCD_data.txt","a");
// fprintf(fp1,"Ieration range:%d\n M N GCD\n",i);
// fprintf(fp1,"%d %d %d\n",tm,tn,m);
// fclose(fp1);
} // k loop
} // j loop
fp2 = fopen("euclid_W.txt","a");
fprintf(fp2,"%d %d\n",i,E_Maxcount);
fclose(fp2);
fp3 = fopen("euclid_B.txt","a");
fprintf(fp3,"%d %d\n",i,E_Mincount);
fclose(fp3);
fp4 = fopen("Meuclid_W.txt","a");
fprintf(fp4,"%d %d\n",i,ME_Maxcount);
fclose(fp4);
fp5 = fopen("Meuclid_B.txt","a");
fprintf(fp5,"%d %d\n",i,ME_Mincount);
fclose(fp5);
fp6 = fopen("con_W.txt","a");
fprintf(fp6,"%d %d\n",i,CI_Maxcount);
fclose(fp6);
fp7 = fopen("con_B.txt","a");
fprintf(fp7,"%d %d\n",i,CI_Mincount);
fclose(fp7);
} // i loop
}