/
main.c
236 lines (227 loc) · 11.9 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/******************************//**/
struct list_el { /**/
int val; /**/
struct list_el * next; /**/
}; /**/
/**/
typedef struct list_el item; /**/
/******************************//**/
item * curr, * head; /**/
/******************************//**/
/******************************//**/
struct list_e2 { /**/
int val; /**/
struct list_e2 * next; /**/
}; /**/
/**/
typedef struct list_e2 item2; /**/
/******************************//**/
item2 * curr2, * head2; /**/
/******************************//**/
int main()
{
head2 = NULL;
int a, number;
printf("Hello world!\n");
printf("Please, enter a number:");
scanf("%d",&number);
printf("Our number is %d\n", number);
a = integers_that_divide_me(number);
getchar();
printf("Integers that divide %d consist of the following:\n",number);
printf(" __________\n");
curr2 = head2;
while(curr2)
{
printf("| %d \n", curr2->val);
curr2 = curr2->next ;
}
printf("|__________\n");
printf("Finished with main()");
return 0;
}
/**************************************************************/
int is_prime(int x) /**/
{ /**/
int counter; /**/
if (x<=1) /**/
{ /**/
return 0; /**/
} /**/
if( x%2==0 ) /**/
{ /**/
if( x==2 )//Is prime! /**/
{ /**/
return 1; /**/
} /**/
return 0;//Else, not prime! /**/
} /**/
if( x%3==0 ) /**/
{ /**/
if( x==3 )//Is prime! /**/
{ /**/
return 1; /**/
} /**/
return 0;//Else, not prime! /**/
} /**/
for (counter=3; counter<=(sqrt(x)+1);counter+=2) /**/
{ /**/
if( (x % counter) == 0 )//Not Prime! /**/
{ /**/
return 0; /**/
} /**/
} /**/
return 1; //Is prime! /**/
}//End of is_prime(x) /**/
/**************************************************************/
/******************************************************************************************************************/
int integers_that_divide_me(int number) /**/
{ /**/
head = NULL; /**/
int counter, candidates,i,x,tempz,indexed_node,count,bewl; /**/
// candidates will be used as a countdown, counter will be used for loops /**/
pool_of_primes(number); // Creating a list of the primes that compose our number /**/
printf("We are seeing what is created from our pool of primes\n\n"); /**/
printf(" __________\n"); /**/
curr = head; /**/
while(curr) /**/
{ /**/
printf("| %d \n", curr->val); /**/
curr = curr->next ; /**/
} /**/
printf("|__________\n"); /**/
getchar(); /**/
/***************************************************************************************************************/
item* temp = head; // assigning a temp node to the head of 1st list to find the size of prime pool /**/
candidates = 0; /**/
i = 0; /**/
tempz = 1; /**/
/**/
int length = 0; /**/
while(temp) /**/
{ /**/
for (i=0;i<length;i++){ /**/
tempz = tempz*2; /**/
} /**/
candidates += tempz; /**/
length++; /**/
temp = temp->next; // move to next node /**/
tempz = 1; /**/
} /**/
/***************************************************************************************************************/
/*We now have a length*/ /**/
if (length >=21) // If there are to many integers /**/
{ /**/
printf("\nTo many possible candidates for integers that divide our number.\n"); /**/
return 0; /**/
} /**/
/***************************************************************************************************************/
/*nList.append(1): while loop does not provide this in our list*/ /**/
curr2 = (item2 *)malloc(sizeof(item2)); /**/
curr2->val = 1; /**/
curr2->next = head2; /**/
head2 = curr2; /**/
/***************************************************************************************************************/
while (candidates != 0) /**/
{ /**/
x = 1; /**/
bewl = 1; /**/
for (counter=0; counter<=length;counter++) /**/
{ /**/
if(candidates & (1<<counter)) /**/
{ /**/
curr = head; /**/
count = 0; /**/
while (curr) /**/
{ /**/
if (count == counter) /**/
{ /**/
indexed_node = curr->val; /**/
x = x * indexed_node; /**/
} /**/
count++; /**/
curr = curr->next; /**/
} /**/
} /**/
} /**/
curr2 = head2; /**/
while(curr2) /**/
{ /**/
if(x == curr2->val) /**/
{ /**/
bewl = 0; /**/
} /**/
curr2 = curr2->next ; /**/
} /**/
if (bewl == 1) /**/
{ /**/
curr2 = (item2 *)malloc(sizeof(item2)); /**/
curr2->val = x; /**/
curr2->next = head2; /**/
head2 = curr2; /**/
} /**/
candidates -= 1; /**/
} /**/
/***************************************************************************************************************/
return 1; /**/
} /**/
/******************************************************************************************************************/
/**************************************************************/
int pool_of_primes(int x) /**/
{ /**/
int i; /**/
if (x <= 0) /**/
{ /**/
if (x == 0) /**/
{// x cannot be divided anything definitely /**/
return 0; /**/
} /**/
else /**/
{ /**/
x = (x/(-1)); /**/
curr = (item *)malloc(sizeof(item)); /**/
curr->val = -1; /**/
curr->next = head; /**/
head = curr; /**/
return pool_of_primes(x); /**/
} /**/
} /**/
if (is_prime(x) == 1) /**/
{ /**/
curr = (item *)malloc(sizeof(item)); /**/
curr->val = x; /**/
curr->next = head; /**/
head = curr; /**/
return 1; /**/
} /**/
else /**/
{ /**/
if ((x % 2) == 0) /**/
{ /**/
x = (x/2); /**/
curr = (item *)malloc(sizeof(item)); /**/
curr->val = 2; /**/
curr->next = head; /**/
head = curr; /**/
return pool_of_primes(x); /**/
} /**/
} /**/
//for i in range(3,int(x**.5)) /**/
for (i=3;i<=(sqrt(x)+1);i+=2) /**/
{ /**/
if((x % i) == 0) /**/
{ /**/
x = (x/i); /**/
curr = (item *)malloc(sizeof(item)); /**/
curr->val = i; /**/
curr->next = head; /**/
head = curr; /**/
return pool_of_primes(x); /**/
} /**/
} /**/
printf("DONE WITH CREATING POOL\n");
}//End of pool_of_primes(x) /**/
/**************************************************************/