-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperUglyNumber.c
247 lines (177 loc) · 4.79 KB
/
SuperUglyNumber.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<unistd.h>
//void initArray();
void options();
void operation(int);
void initSet();
void check(unsigned long);
void checkPrime();
void bubbleSort();
char getch();
//int *arr;
int *set = NULL;
unsigned int /*size,*/setSize;
unsigned int total;
void main(){
initSet();
while(1){
system("clear");
total=0;
options();
}
free(set);
set = NULL;
}
void initSet(){
unsigned int i;
if(set!=NULL){
free(set);
set = NULL;
}
printf("\nEnter the size of set ");
scanf("%d",&setSize);
set = malloc(sizeof(int)*setSize);
printf("\nEnter the set now (remember to provide prime)\n");
for(i=0;i<setSize;i++)
scanf("%d",&set[i]);
checkPrime();
bubbleSort();
}
void options(){
int choice;
printf("\nChoose from the following operations.....\n");
printf("\n1.Check whether the given number is super ugly or not");
printf("\n2.Check the total number of super ugly number in given range");
printf("\n3.n'th ugly number in given set");
printf("\n4.Enter new set");
printf("\n5.exit");
printf("\nyour choice ");
scanf("%d",&choice);
operation(choice);
}
void operation(int choice){
unsigned long num;
int start,end,temp,i;
system("clear");
switch(choice){
case 1:
printf("\nEnter the number you want to check ");
scanf("%d",&num);
check(num);
if(total==1)
printf("\n%ld is a super ugly number for the given set",num);
else
printf("\n%ld is not an ugly number",num);
break;
case 2:
printf("\nEnter the range with space in between start and end ");
scanf("%d%d",&start,&end);
if(start>end){
temp = start;
start = end;
end = temp;
}
for(i = start;i<=end;i++)
check(i);
printf("\nThere are %d super ugly number in range of %d to %d",total,start,end);
break;
case 3:
printf("\nEnter the n'th number ");
scanf("%d",&temp);
i = 1;
while(total!=temp){
check(i);
i++;
}
printf("\nn'th number is %d",--i);
break;
case 4:
free(set);
set = NULL;
initSet();
break;
case 5:
exit(0);
default:
printf("!!!...Enter only from the given operation..!");
}
getch();
}
/*void initArray(){
printf("\nEnter the total set of number's ");
scanf("%d",&size);
arr = malloc(sizeof(int)*size);
}*/
void check(unsigned long num){
int i = 0;
while(i<setSize){
if(num==1){
total++;
//printf("\n%d %d",num,total);
return;
}
else if(num%set[i]==0){
//printf("\n%d",num);
check(num/set[i]);
return;
}
i++;
}
}
void checkPrime(){
int i = 0,j;
int flag = 0;
while(i<setSize){
for(j=2;j<=set[i]/2;j++){
if(set[i]%j==0){
flag = 1;
printf("\n%d is not a prime number",set[i]);
break;
}
}
i++;
}
if(flag==1){
printf("\nPlease reinitialize your set with proper value .....");
getch();
initSet();
}
}
void bubbleSort(){
int i,j,temp;
for(i=0;i<setSize-1;i++){
for(j=0;j<setSize-i-1;j++){
if(set[j]>set[j+1]){
temp = set[j];
set[j] = set[j+1];
set[j+1] = temp;
}
}
}
}
//Copy code for implementation of getch function......
char getch(){
/*#include <unistd.h> //_getch*/
/*#include <termios.h> //_getch*/
char buf=0;
struct termios old={0};
fflush(stdout);
if(tcgetattr(0, &old)<0)
perror("tcsetattr()");
old.c_lflag&=~ICANON;
old.c_lflag&=~ECHO;
old.c_cc[VMIN]=1;
old.c_cc[VTIME]=0;
if(tcsetattr(0, TCSANOW, &old)<0)
perror("tcsetattr ICANON");
if(read(0,&buf,1)<0)
perror("read()");
old.c_lflag|=ICANON;
old.c_lflag|=ECHO;
if(tcsetattr(0, TCSADRAIN, &old)<0)
perror ("tcsetattr ~ICANON");
printf("%c\n",buf);
return buf;
}