-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacs.c
More file actions
316 lines (191 loc) · 6.41 KB
/
pacs.c
File metadata and controls
316 lines (191 loc) · 6.41 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
309
310
311
312
313
314
315
316
/** PRIME NUMBERS AND COMPOSITE NUMBERS SUBSETS BETWEEN FIBONACCI WORDS by MARTE.BEST - Sylwester Bogusiak. 2019 AD v 0.9 beta **/
// Check how each subset of Primes resonate with Golden Number Phi= 1.618033... The Arithmetic Mean aka Average divided by upper Fibonacci from border = F(n) is near to, but lesser than Phi / 2... = 0.89... and closer with bigger values of Primes
// But Composite Numbers Average divided by Fib(n+1) is greater than Phi /2
// More info in my publication available on Researchgate.net: Arithmetic mean of subsets of prime numbers. A method that counts the convergence to the golden number Phi.
// See also this thread on MATHFORUMS.COM: https://mathforums.com/t/arithmetic-mean-of-subsets-of-prime-numbers-a-method-that-counts-the-convergence-to-the-golden-number-phi.370710/
// PRIMES AND COMPOSITE SUBSETS
// PACS algorithm - Primes And Composite Subsets
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate Fibonacci numbers and Prime Numbers and Composite Numbers
// There is no warranty or guarantee of any kind.
// To Compile:
// gcc -o pacs pacs.c -lm
// Usage in command line:
// ./pacs 10 30
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#define Phi 1.618033988749895
unsigned long long fib(int n) {
if ((n == 1) || (n == 2))
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main( int argc, const char* argv[] )
{
time_t start;
time_t end; // Get the system time
bool print = false;
int x;
int y;
unsigned long long i,j,k;
unsigned long long sum_p=0; /// sum of prime numbers
unsigned long long qty_p=0; /// qty of primes numbers in subset
unsigned long long sum_c=0; /// sum of composite numbers
unsigned long long qty_c=0; /// qty of composite numbers in subset
unsigned long long limit; /// limit of primes from fib
unsigned long long subset_start=0; ///
unsigned long long subset_end=1;
unsigned long long tempNumber=0;
double approx_p_phi;
double approx_c_phi;
double avg_p=0;
double avg_c=0;
qty_p=0;
qty_c=0;
if ( argc >= 3 )
{
x = atoi( argv[1] );
y = atoi( argv[2] );
if ( argc == 4 )
{
if (!(strcmp(argv[3],"print")))
{
print = true;
}
}
time(&start); // Get the system time
if ((y>x) && (x>=4))
{
printf("________________________________________________________________________\n");
printf("____________THAT APP IS CALCULATING THE PRIME NUMBERS SUBSETS___________\n");
printf("_______________________AND COMPOSITE NUMBERS SUBSETS____________________\n");
printf("____________BETWEEN FIBONACCI AND FINDING APPROXIMATION_________________\n");
printf("______________________TO THE GOLDEN NUMBER_PHI._________________________\n");
printf("___________Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91__________\n");
printf("________________________________________________________________________\n");
printf(" Please wait for prime numbers generator. Warning! For second argument f2 greater than 45 this may take couple minutes. Bigger f2 consume more and more time and memory.\n");
limit = fib(y); /// only once
// Allocate memory for prime array and initialize all
// elements as true
bool* prime = malloc((limit + 1) * sizeof(bool));
for (int i = 0; i <= limit; i++)
prime[i] = true;
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
// For each number from 2 to sqrt(n)
for (int p = 2; p <= sqrt(limit); p++) {
// If p is prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= limit; i += p)
prime[i] = false;
}
}
if (print)
{
// Print all prime numbers up to n
printf("Prime numbers up to %lld:\n", limit);
for (int p = 2; p <= limit; p++) {
if (prime[p])
printf("%d ", p);
}
printf("\n");
}
subset_start=fib(x-1); /// For start
subset_end=fib(x); ///
for (i=x;i<y;i++) /// Main loop
{
/// Fibo iteration
tempNumber = subset_start;
subset_start = subset_end;
subset_end = tempNumber + subset_start;
/// Zero values for new subset
sum_p = 0;
qty_p = 0;
sum_c = 0;
qty_c = 0;
avg_p = 0;
approx_p_phi = 0;
avg_c = 0;
approx_c_phi = 0;
if (print)
{
printf("\n Fib(%llu) = %llu - Fib(%llu) = %llu ",i, subset_start,i+1,subset_end);
printf("\n Primes and composite list: \n");
}
for (j = subset_start+1; j <= subset_end; j++) /// Count every single element
{
if (prime[j])
{
if (print)
printf(" p%llu ",j); /// Print if you need
sum_p += j;
qty_p += 1;
}
else
{
if (print)
printf(" n%llu ",j); /// Print if you need
sum_c +=j;
qty_c +=1;
}
}
if (j>=4)
{
avg_p = sum_p/qty_p;
avg_c = sum_c/qty_c;
approx_p_phi = ((avg_p/subset_end) *2);
approx_c_phi = ((avg_c/subset_end) *2);
printf("\n");
printf(" Fib(%llu) = %llu - Fib(%llu) = %llu SUM P = %llu QTY P = %llu AVG P = %.2f Phi P = %f\n",i,subset_start,i+1, subset_end, sum_p, qty_p, avg_p, approx_p_phi);
printf(" Fib(%llu) = %llu - Fib(%llu) = %llu SUM C = %llu QTY C = %llu AVG C = %.2f Phi C = %f\n",i,subset_start,i+1, subset_end, sum_c, qty_c, avg_c, approx_c_phi);
printf("\n");
if (approx_p_phi > Phi)
{
printf(" Phi P = %f > Phi\n", approx_p_phi);
}
else
if (approx_p_phi < Phi)
{
printf(" Phi P = %f < Phi\n", approx_p_phi);
}
else
if (approx_p_phi == Phi)
{
printf(" Phi P = %f = Phi\n", approx_p_phi);
}
if (approx_c_phi > Phi)
{
printf(" Phi C = %f > Phi\n", approx_c_phi);
}
else
if (approx_c_phi < Phi)
{
printf(" Phi C = %f < Phi\n", approx_c_phi);
}
else
if (approx_c_phi == Phi)
{
printf(" Phi C = %f = Phi\n", approx_c_phi);
}
printf("\n");
}
}
// Free allocated memory
free(prime);
time(&end); // End time
double dif;
dif = difftime (end,start); // time difference
printf ("Your calculations took %.2lf seconds to run.\n", dif ); //
return 0;
}
} // if
printf(" Simple usage: pacs f1 f2 \n where f1 and f2 are natural numbers and Fibonacci words in sequence Fib(n) Fib(n+1) and f1 >= 4 and f2 > f1\n Warning! f2 should not exceed 50 for 8 GB RAM otherwise you will reach memory limit.\n");
printf(" Additionally to print all natural numbers use: pacs f1 f2 print\n");
return 0;
}