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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
#include <stdlib.h>
#include <math.h>
#include "utils.h"
/* http://www.evanmiller.org/mathematical-hacker.html */
static long int _fib(unsigned long int n)
{
return lround((pow(0.5 + 0.5 * sqrt(5.0), n) -
pow(0.5 - 0.5 * sqrt(5.0), n)) / sqrt(5.0));
}
/* http://blog.noblemail.ca/2013/01/on-calculating-fibonacci-numbers-in-c.html */
long int fib(unsigned long int n)
{
return lround((pow(0.5 + 0.5 * sqrt(5.0), n)) / sqrt(5.0));
}
/* factorial */
long int fac(unsigned long int n) {
return lround(exp(lgamma(n+1)));
}
list_t *list_append(list_t *list, void *value)
{
list_t *ret, *curr;
ret = malloc(sizeof(list_t));
ret->value = value;
ret->next = NULL;
if (!list)
{
return ret;
}
curr = list;
while (curr->next)
{
curr = curr->next;
}
curr->next = ret;
return list;
}
list_t *list_append_int(list_t *list, int i)
{
int *p = malloc(sizeof(int));
*p = i;
return list_append(list, p);
}
list_t *list_append_long(list_t *list, long l)
{
long *p = malloc(sizeof(long));
*p = l;
return list_append(list, p);
}
int list_len(list_t *list)
{
int i = 0;
while (list)
{
list = list->next;
++i;
}
return i;
}
void *list_get_n(list_t *list, int n)
{
int i = 0;
for (; list && i < n; ++i)
{
list = list->next;
}
if (list)
return list->value;
return NULL;
}
void list_append_list(list_t *list, list_t *elem)
{
while (list->next)
{
list = list->next;
}
list->next = elem;
}
void list_sort(list_t **list, list_compare_cb cmp)
{
int len = list_len(*list);
list_t *pivot = *list;
list_t *less = NULL, *greater = NULL;
list_t *curr;
if (len <= 1)
return;
curr = pivot->next;
pivot->next = NULL;
while (curr)
{
list_t *next = curr->next;
curr->next = NULL;
if (cmp(curr->value, pivot->value) <= 0)
{
if (!less)
less = curr;
else
list_append_list(less, curr);
}
else
{
if (!greater)
greater = curr;
else
list_append_list(greater, curr);
}
curr = next;
}
list_sort(&less, cmp);
list_sort(&greater, cmp);
list_append_list(less, pivot);
list_append_list(less, greater);
*list = less;
}
void list_free_int(void *value)
{
free((int*)value);
}
void list_free_long(void *value)
{
free((long*)value);
}
void list_free(list_t *list, list_free_cb cb)
{
list_t *tmp;
if (!list)
{
return;
}
while (list->next)
{
cb(list->value);
tmp = list;
list = list->next;
free(tmp);
}
cb(list->value);
free(list);
}
list_t *prime_factors_naive(const long *prime_table, long prime_table_size,
long number)
{
long prime = 0;
long i;
char quot_is_prime;
list_t *factors = NULL;
// printf("table size: %ld\n", prime_table_size);
while (prime < prime_table_size)
// while (1)
{
// printf("prime: %ld\n", prime);
long quot = number / prime_table[prime];
long rem = number % prime_table[prime];
if (rem == 0)
{
// printf("factor: %ld\n", prime_table[prime]);
factors = list_append_long(factors, prime_table[prime]);
if (quot == 1)
{
break;
}
quot_is_prime = 1;
for (i = 2; i < sqrt(quot); ++i)
{
if (quot % i == 0)
{
quot_is_prime = 0;
break;
}
}
if (quot_is_prime)
{
// printf("factor: %ld\n", quot);
factors = list_append_long(factors, quot);
break;
}
number = quot;
prime = 0;
}
else
{
prime++;
}
}
return factors;
}
long int gcd(long int a, long int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
while (1)
{
long int q = a / b;
long int r = a % b;
if (r == 0)
{
return b;
}
a = b;
b = r;
}
}
long int lcm(long int a, long int b)
{
a = a / gcd(a, b);
return a * b;
}
int is_coprime(long int a, long int b)
{
if (gcd(a, b) == 1)
{
return 1;
}
return 0;
}
int pythagorean_triplet(int p, int q, int *a, int *b, int *c)
{
// http://www.friesian.com/pythag.htm
// Two cases:
//
// 1. p and q must both be odd, p>q, gcd(p,q)=1
// 2. p and q must not both be odd, p>q, gcd(p,q)=1
if (p > q && gcd(p, q) == 1)
{
if (p % 2 != 0 && q % 2 != 0)
{
*a = p*q;
*b = (p*p - q*q) / 2;
*c = (p*p + q*q) / 2;
// sometimes a > b, we can just switch
if (*a > *b)
{
p = *a;
*a = *b;
*b = p;
}
return 1;
}
else if ((p % 2 == 0 && q % 2 != 0) || (p % 2 != 0 && q % 2 == 0))
{
*a = 2*p*q;
*b = p*p - q*q;
*c = p*p + q*q;
// sometimes a > b, we can just switch
if (*a > *b)
{
p = *a;
*a = *b;
*b = p;
}
return 1;
}
}
return 0;
}
|