-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecialNumbers.c
More file actions
308 lines (257 loc) · 6.96 KB
/
Copy pathspecialNumbers.c
File metadata and controls
308 lines (257 loc) · 6.96 KB
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
/*specialNumbers
Copyright (C) 2013 Michel Dubois
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define couleur(param) printf("\033[%sm",param)
#define MAXSAMPLE 30000000
static unsigned long long tempTab[MAXSAMPLE];
static unsigned long long listNumbers[MAXSAMPLE];
static int sampleSize=0, size=0;
void usage(void) {
couleur("31");
printf("Michel Dubois -- specialNumbers -- (c) 2013\n\n");
couleur("0");
printf("Syntaxe: specialNumbers <name> <sample size>\n");
printf("\t<name> -> name of the algorithm\n");
printf("\t\tavalaible algorithms are: prime, semiprime, blum, euler, fibonacci, fredkin\n");
printf("\t<sample size> -> sample size\n");
}
unsigned long long factorial(unsigned long long n) {
unsigned long long i=0, result=1;
for (i=1; i<=n; i++) {
result *= i;
}
return(result);
}
unsigned long long gcd(unsigned long long n1, unsigned long long n2) {
unsigned long long tmp;
while (n1 != 0) {
tmp = n1;
n1 = n2 % n1;
n2 = tmp;
}
return n2;
}
unsigned long long eulerTotient(unsigned long long n) {
// www.answers.com/topic/euler-s-totient-function
unsigned long long result = 0, k = 0;
for (k=0; k<n; k++) {
if (gcd(k,n) == 1)
result++;
}
return result;
}
void printData(void) {
int i;
for (i=0; i<size; i++) {
printf("%lli, ", listNumbers[i]);
}
printf("\n%d\n", size);
}
void saveData(void) {
int i;
FILE *fd = fopen("result.dat", "w");
if (fd != NULL) {
printf("INFO: file create\n");
for (i=0; i<size; i++) {
fprintf(fd, "%lli\n", listNumbers[i]);
}
fclose(fd);
printf("INFO: file close\nINFO: data saved in result.dat\n");
} else {
printf("INFO: open error\n");
}
}
void bubbleSort(void) {
int i, j, t;
for (i=0; i<size; i++) {
tempTab[i] = listNumbers[i];
}
for (i=size-1; i>=0; i--) {
for (j=0; j<i; j++) {
if(tempTab[j] > tempTab[j+1]) {
t = tempTab[j];
tempTab[j] = tempTab[j+1];
tempTab[j+1] = t;
}
}
}
for (i=0; i<size; i++) {
listNumbers[i] = tempTab[i];
}
}
void cribleEratosthene(int n) {
int i, max=round(sqrt(n)), cpt=0, nbrPrime=0;
// initialisation du tableau
tempTab[0] = 0;
tempTab[1] = 0;
for (i=2; i<n; i++) {
tempTab[i] = 1;
}
// recherche des nombres premiers
for (i=2; i<=max; i++) {
if (tempTab[i] == 1) {
cpt = i;
while (i*cpt<=n) {
tempTab[i*cpt] = 0;
cpt++;
}
}
}
// calcul du nombre de nombre premier trouvé
for (i=0; i<n; i++) {
if (tempTab[i] == 1) {
nbrPrime++;
}
}
// écriture du tableau de nombres premiers
cpt=0;
for (i=0; i<n; i++) {
if (tempTab[i] == 1) {
listNumbers[cpt] = i;
cpt++;
}
}
size = nbrPrime;
}
void primeNumber(int n) {
// affiche la liste des nombres premiers <n
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
// http://mathworld.wolfram.com/SieveofEratosthenes.html
printf("Prime numbers (https://oeis.org/A000040) less than %d\n", n);
printf("A number n is prime if it is greater than 1 and has no positive divisors except 1 and n.\n");
cribleEratosthene(n);
}
void semiPrimeNumber(int n) {
// affiche la liste des nombres semi premiers <n
// https://en.wikipedia.org/wiki/Semiprime
// http://mathworld.wolfram.com/Semiprime.html
int i, j, tmp, cpt=0;
printf("Semiprimes (https://oeis.org/A001358) less than %d\n", n);
printf("Numbers of the form p*q where p and q are primes, not necessarily distinct.\n");
cribleEratosthene(n);
for (i=0; i<size; i++) {
for (j=i; j<size; j++) {
tmp = listNumbers[i] * listNumbers[j];
if (tmp <= n) {
tempTab[cpt] = tmp;
cpt++;
}
}
}
for (i=0; i<cpt; i++) {
listNumbers[i] = tempTab[i];
}
size = cpt;
bubbleSort();
}
void blumNumber(int n) {
// affiche la liste des nombres de Blum <n
// Blum numbers: of form p*p where p & q are distinct primes congruent to 3 (mod 4)
// https://en.wikipedia.org/wiki/Blum_integer
// http://www.gilith.com/research/talks/cambridge1997.pdf
// http://cseweb.ucsd.edu/~mihir/papers/gb.pdf
int i, j, cpt=0, val=0;
printf("Blum numbers (https://oeis.org/A016105) less than %d\n", n);
printf("nNumbers of the form p * q where p and q are distinct primes congruent to 3 (mod 4).\n");
cribleEratosthene(n);
for (i=0; i<size; i++) {
for (j=i+1; j<size; j++) {
if ((listNumbers[i] % 4 == 3) && (listNumbers[j] % 4 == 3)) {
val = listNumbers[i] * listNumbers[j];
if (val <= n) {
tempTab[cpt] = val;
cpt++;
}
}
}
}
for (i=0; i<cpt; i++) {
listNumbers[i] = tempTab[i];
}
size = cpt;
bubbleSort();
}
void eulerNumber(int n) {
// Euler totient function phi(n): count numbers <= n and prime to n.
// https://en.wikipedia.org/wiki/Euler%27s_totient_function
// http://mathworld.wolfram.com/TotientFunction.html
int i;
printf("Totient numbers (https://oeis.org/A000010) less than %d\n", n);
for (i=0; i<n; i++) {
listNumbers[i] = eulerTotient(i+1);
}
size = i;
}
void fibonacciNumber(int n) {
int i;
printf("Fibonacci numbers (https://oeis.org/A000045)\n");
for (i=0; i<n; i++) {
if (i==0) { listNumbers[i] = 0; }
else if (i==1) { listNumbers[i] = 1; }
else { listNumbers[i] = listNumbers[i-1] + listNumbers[i-2]; }
}
size = i;
}
void fredkinNumber(int n) {
int i;
printf("Fredkin replicator (https://oeis.org/A160239)\n");
listNumbers[0] = 1;
for (i=0; i<n; i++) {
listNumbers[2*i] = listNumbers[i];
listNumbers[4*i+1] = 8 * listNumbers[i];
listNumbers[4*i+3] = 2 * listNumbers[2*i+1] + 8 * listNumbers[i];
}
size = i;
}
int main(int argc, char *argv[]) {
switch (argc) {
case 3:
sampleSize = atoi(argv[2]);
if (sampleSize<=MAXSAMPLE) {
if (!strncmp(argv[1], "prime", 5)) {
primeNumber(sampleSize);
} else if (!strncmp(argv[1], "semiprime", 10)) {
semiPrimeNumber(sampleSize);
} else if (!strncmp(argv[1], "blum", 4)) {
blumNumber(sampleSize);
} else if (!strncmp(argv[1], "euler", 5)) {
eulerNumber(sampleSize);
} else if (!strncmp(argv[1], "fibonacci", 9)) {
fibonacciNumber(sampleSize);
} else if (!strncmp(argv[1], "fredkin", 7)) {
fredkinNumber(sampleSize);
} else {
usage();
exit(EXIT_FAILURE);
break;
}
printData();
saveData();
exit(EXIT_SUCCESS);
} else {
printf("Sample size exceeded (authorized max sample: %d)\n", MAXSAMPLE);
usage();
exit(EXIT_FAILURE);
}
break;
default:
usage();
exit(EXIT_FAILURE);
break;
}
return 0;
}