-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyb.c
247 lines (216 loc) · 4.72 KB
/
polyb.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
243
244
245
246
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "check.h"
#define HEAP 1
#define RECHEAP 2
#define PRUNEDHEAP 3
#define PERMUTE 4
void checkpoly(uint32_t *n, uint32_t *a, uint32_t b)
{
uint32_t i;
if (ispoly(n,a,b) == 0)
{
printf("Polydivisible number in base %u!!!: ",b);
for (i = 0; i < b-1; i++)
{
printf("%u ",n[i]);
}
printf("\n");
}
}
void permute(uint32_t l, uint32_t *n, uint32_t *a, uint32_t b)
{
uint32_t temp, i;
// l is the length of the (sub)sequence to permute
if (ispartpoly(n,a,b,b-l-1) != 0)
{
return;
}
if (l == 1)
{
if (ispartpoly(n,a,b,b-1) == 0)
{
printf("Polydivisible number in base %u!!!: ",b);
for (i = 0; i < b-1; i++)
{
printf("%u ",n[i]);
}
printf("\n");
}
return;
}
// First permute the remaining digits
permute(l-1, n, a, b);
// Try all possible digits not already used
for (i = 0; i < l-1; i++)
{
// swap the current digit with each down the sequence
temp = n[b-l-1];
n[b-l-1] = n[b-l+i];
n[b-l+i] = temp;
permute(l-1,n,a,b);
// swap back
temp = n[b-l-1];
n[b-l-1] = n[b-l+i];
n[b-l+i] = temp;
}
}
void generate(uint32_t l, uint32_t *n, uint32_t *a, uint32_t b)
{
// l is the length of the (sub)sequence to permute
uint32_t c, temp, j;
if (l == 1)
{
checkpoly(n,a,b);
return;
}
// Heap's algorithm with array indices inverted
// So swapping from the end of the array most
// frequently
for (c = 0; c < l; c++)
{
generate(l-1, n, a, b);
j = l % 2 ? 0 : c;
temp = n[b-2-j];
n[b-2-j] = n[b-l-1];
n[b-l-1] = temp;
}
}
void generateprune(uint32_t l, uint32_t *n, uint32_t *a, uint32_t b)
{
// l is the length of the (sub)sequence to permute
uint32_t c, temp, j, i;
//if (n[1] == 3) return;
/*if (ispartpoly(n,a,b,b-l) != 0)
{
return;
}
if (l == 1)
{
// If we reach here and just tested for divisibility by b-1 then
// we've found a polydivisible number
printf("Polydivisible number in base %u!!!: ",b);
for (i = 0; i < b-1; i++)
{
printf("%u ",n[i]);
}
printf("\n");
return;
}*/
if (l == 1)
{
for (i = 0; i < b-1; i++)
{
printf("%u ",n[i]);
}
printf("\n");
checkpoly(n,a,b);
return;
}
// Heap's algorithm with array indices inverted
// So swapping from the end of the array most
// frequently
for (c = 0; c < l; c++)
{
generateprune(l-1, n, a, b);
j = l % 2 ? 0 : c;
temp = n[b-2-j];
n[b-2-j] = n[b-l-1];
n[b-l-1] = temp;
}
}
int main(int argc, char **argv)
{
if (argc != 2 && argc != 3)
{
printf("Usage: ./poly base [1=bruteforce|2=heap|3=prunedheap]\n");
exit(1);
}
uint32_t b = atoi(argv[1]);
int32_t i, j;
uint32_t alg = PERMUTE;
if (argc == 3)
{
alg = atoi(argv[2]);
}
printf("A program for finding polydivisible numbers\n");
printf("By Iain Bethune\n\n");
printf("Checking base %u ...\n",b);
if (b % 2 == 1)
{
printf("Odd base => No polydivisible numbers!\n");
return 0;
}
uint32_t *a = (uint32_t *)malloc((b-1)*sizeof(uint32_t));
uint32_t *n = (uint32_t *)malloc((b-1)*sizeof(uint32_t));
if (alg == HEAP)
{
// Generate all permutations...
uint32_t temp;
uint32_t *p = (uint32_t *)malloc((b-1)*sizeof(uint32_t));
for (i = 0; i < b-1; i++)
{
n[i] = i + 1;
p[i] = 0;
}
checkpoly(n,a,b);
i = 1;
while (i < b-1)
{
if (p[i] < i)
{
j = i % 2 * p[i];
temp = n[j];
n[j] = n[i];
n[i] = temp;
checkpoly(n,a,b);
p[i]++;
i = 1;
}
else
{
p[i] = 0;
i++;
}
}
free(p);
}
else if (alg == RECHEAP)
{
for (i = 0; i < b-1; i++)
{
n[i] = i + 1;
}
// permute all b-1 digits
generate(b-1, n, a, b);
}
else if (alg == PRUNEDHEAP)
{
printf("Recursive heap not implemented...\n");
/*
for (i = 0; i < b-1; i++)
{
n[i] = i + 1;
}
// permute all b-1 digits
generateprune(b-1, n, a, b);
*/
}
else if (alg == PERMUTE)
{
for (i = 0; i < b-1; i++)
{
n[i] = i + 1;
}
// permute all b-1 digits
permute(b-1, n, a, b);
}
else
{
printf("Error: algorithm %u not recognised!\n",alg);
}
printf("Done!\n");
free (a); free(n);
return 0;
}