-
Notifications
You must be signed in to change notification settings - Fork 1
/
code-appendix.txt
372 lines (351 loc) · 8.04 KB
/
code-appendix.txt
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <math.h>
#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void leibniz();
void circle();
void wallis();
void ramanujan(unsigned long long times);
void ramanujanNW(int times);
void ramd(int times);
void factz();
void circleD(unsigned long long size);
int inCircle(unsigned long long a, unsigned long long b, unsigned long long size);
void ramanjuanRat(unsigned long long times);
void monteCarlo(unsigned long long times);
void chudnovsky(unsigned long long times);
void newton(unsigned long long times);
void doubFactz(unsigned long long x);
mpz_t z,f;
mpf_t times, n;
mpz_t radius;
int main(){
//mpf_inp_str(
unsigned long long clo = clock();
//printf("%lu\n",ULONG_MAX);
//for things using mpf
/*mpf_init2(times,1024);
mpf_init2(n,1024);
mpf_set_ui(times,1);//enter terms/1000
mpf_mul_ui(times,times,100);*/
//mpf_set_ui(times,10);//enter terms/1000
//mpf_mul_ui(times,times,1000);
//mpf_init2(n,1024);
//for things using mpz
mpz_init(radius);
mpz_set_ui(radius,128);//enter terms/1000
mpz_mul_ui(radius,radius,1000);
//ramanujan(6400);
//newton(16000);
chudnovsky(6400);
//leibniz();
//wallis();
//circle();
//unsigned long long numterms = 128000;
//circle(numterms);
//mpz_init(z);
//chudnovsky(1);
//mpz_init(z);
//mpz_set_ui(z,1);
//doubFactz(5);
//printf("%d\n", mpz_get_ui(z));
//ramd(5);
//ramanujan(5);
//ramanjuanRat(5);
//mpf_init2(z,1024);
//mpf_set_ui(z,1);
//factz(5);
//leibniz(numterms);
/*mpf_t x;
mpf_init2(x,1024);
mpf_set_d(x,M_PI);
mpf_out_str(stdout, 10, 0, x);
printf("\n");*/
//wallis(100000);
//mpf_t x;
//mpf_init2(x, 1024);
//mpf_set_ui(x,100000);
//mpf_add_ui(x, x, 3);
//mpf_out_str(stdout,10,0,z);
//printf("\n");
//circle(4800);
//circleD(100000);
//monteCarlo(100000);
//newton(1000);
printf("%ld\n",(clock()-clo)/1000);
//printf("Terms:%d\n", numterms);
//sprintf("%e\n", M_PI);
return 0;
}
void leibniz(){
// printf("runtohere\n");
mpf_t pi, term,numer;
mpf_init2(pi,1024);
mpf_init2(term,1024);
mpf_init2(numer,1024);
mpf_set_ui(n,0);
//int a;
//for (a=0; a<times; a++){
while(mpf_cmp(n,times)){
//mpf_set_si(numer,(a%2)?(-1):(1));
if (mpf_cmp_ui(numer,0)<=0)
mpf_set_ui(numer,1);
else
mpf_set_si(numer,-1);
//printf("run\n");
//mpf_set_ui(term,(2*a+1));
mpf_set(term,n);
mpf_mul_ui(term,term,2);
mpf_add_ui(term,term,1);
mpf_div(term,numer,term);
mpf_add(pi,pi,term);
/*if (!(a%(times/100))) {
mpf_t pi4;
mpf_init2(pi4, 1024);
mpf_mul_ui(pi4,pi,4);
mpf_out_str(stdout, 10, 0, pi4);
printf("\n");
}*/
//if (!(a%(times/100)))
//printf("%d\n",a/(times/100));
mpf_add_ui(n,n,1);
}
//return pi*4;
mpf_mul_ui(pi, pi, 4);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
}
void circle(){
mpz_t in, temp,temp2,size2,x,y;
//in=in circle
//temp=x^2
//temp2=y^2
//size2=size^2
mpz_init2(in,1024);
mpz_init2(temp,1024);
mpz_init2(temp2,1024);
mpz_init2(size2,1024);
mpz_init2(x,1024);
mpz_init2(y,1024);
mpz_set(size2,radius);
mpz_mul(size2,size2,radius);
//unsigned long long x, y;
//int f=0;//checking that the denom is right
//x=0;
//y=0;
mpz_set_ui(x,0);
mpz_set_ui(y,0);
while (1){
//printf("loop run\n");
//f++;
mpz_set(temp,x);
mpz_mul(temp,temp,x);
mpz_set(temp2,y);
mpz_mul(temp2,temp2,y);
mpz_add(temp,temp,temp2);
if (mpz_cmp(temp,size2)<=0){
mpz_add_ui(in,in,1);
//x++;
mpz_add_ui(x,x,1);
}
else{
mpz_add_ui(y,y,1);
//f+=(size-x);
//y++;
//if (size<y)
if (mpz_cmp(y,radius)>0)
break;
//x=0;
mpz_set_ui(x,0);
}
}
mpf_t pi,tot;
mpf_init2(pi,1024);
mpf_set_z(pi,in);
mpf_init2(tot,1024);
mpf_set_z(tot,radius);
mpf_add_ui(tot,tot,1);
mpf_mul(tot,tot,tot);
//mpz_out_str(stdout, 10, in);
//printf("\n");
//mpf_out_str(stdout, 10, 0, tot);
//printf("\n");
mpf_div(pi,pi,tot);
mpf_mul_ui(pi,pi,4);
//printf("Size:%d\n",size);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
//printf("%d,%d\n",f,(size+1)*(size+1));
}
void wallis(){
mpf_t pi, numer, term,temp;
//mpf_div_ui(times,times,2);
mpf_init2(pi,1024);
mpf_init2(numer,1024);
mpf_init2(term, 1024);
mpf_init2(temp,1024);
mpf_set_ui(pi,1);
mpf_set_ui(n,1);
//int a;
//for (a=1; a<=times/2; a++){
while(mpf_cmp(times,n)){
//printf("run\n");
mpf_set_ui(numer,2);
mpf_mul(numer,numer,n);
mpf_mul(numer,numer,numer);
mpf_set(term,n);
mpf_mul_ui(term,term,2);
mpf_sub_ui(term,term,1);
//mpf_set(pi,
//mpf_mul_ui(term,term,2*a+1);
mpf_set_ui(temp,2);
mpf_mul(temp,temp,n);
mpf_add_ui(temp,temp,1);
mpf_mul(term,term,temp);
mpf_div(term,numer,term);
mpf_mul(pi,pi,term);
//if (!(a%(times/200)))
//printf("%d\n",a*2/(times/100));
mpf_add_ui(n,n,1);
}
mpf_mul_ui(pi, pi, 2);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
}
void ramanujan(unsigned long long times){
long long prec = 256*1024;
mpf_t pi,term,tmp;
mpf_init(pi);
mpf_init(term);
//mpf_init(mult);
mpf_init(tmp);
mpz_init(z);
mpz_set_ui(z,1);
//mpf_init2(rpi,16384);
mpf_set_prec(pi,prec);
mpf_set_prec(term,prec);
//mpf_set_prec(mult,16384);
mpf_set_prec(tmp,prec);
//mpf_set_ui(mult,1);
long long n;
mpf_set_ui(pi,0);
//mpf_out_str(stdout, 10, 0, tmp);
//printf("\n");
for (n=0; n<times; n++){
mpf_set_ui(term,1103);
mpf_set_ui(tmp,26390);
mpf_mul_ui(tmp,tmp,n);
mpf_add(term,term,tmp);
mpz_set_ui(z,1);
factz(4*n);
mpf_set_z(tmp,z);
mpf_mul(term,term,tmp);
mpf_set_ui(tmp,396);
mpf_pow_ui(tmp,tmp,4*n);
mpf_div(term,term,tmp);
mpz_set_ui(z,1);
factz(n);
mpf_set_z(tmp,z);
mpf_pow_ui(tmp,tmp,4);
mpf_div(term,term,tmp);
mpf_add(pi,pi,term);
//mpf_out_str(stdout, 10, 0, term);
//printf("\n");
}//while (mpf_cmp_d(rpi,M_PI)>0);
mpf_sqrt_ui(tmp,2);
mpf_mul_ui(tmp,tmp,2);
mpf_div_ui(tmp,tmp,9801);
//mpf_set_d(tmp,(2*sqrt(2))/9801);
mpf_mul(pi,pi,tmp);
mpf_ui_div(pi,1,pi);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
//mpf_set_d(tmp,M_PI);
//mpf_sub(tmp,pi,tmp);
//printf("delta=\n");
//mpf_out_str(stdout, 10, 0, tmp);
//printf("\n");
mpz_set_ui(z,1);
}
void factz(unsigned long long x){
if (x==1||!x)
return;
mpz_mul_ui(z,z,x);
factz(x-1);
}
void chudnovsky(unsigned long long times){
long long prec = 524288;
mpf_t tmp,pi,term;
mpz_init(z);
mpf_init2(tmp,prec);
//mpf_init2(tmp2,1024);
mpf_init2(pi,prec);
mpf_init2(term,prec);
mpf_set_ui(pi,0);
long long a;
for (a=0; a<times; a++){
mpf_set_ui(term,13591409);
mpf_set_ui(tmp,545140134);
mpf_mul_ui(tmp,tmp,a);
mpf_add(term,term,tmp);
mpz_set_ui(z,1);
factz(6*a);
mpf_set_z(tmp,z);
mpf_mul(term,term,tmp);
//if (a%2)
//mpf_neg(term,term);
mpz_set_ui(z,1);
factz(3*a);
mpf_set_z(tmp,z);
mpf_div(term,term,tmp);
mpz_set_ui(z,1);
factz(a);
mpf_set_z(tmp,z);
mpf_pow_ui(tmp,tmp,3);
mpf_div(term,term,tmp);
mpf_set_si(tmp,-640320);
mpf_pow_ui(tmp,tmp,3*a);
mpf_div(term,term,tmp);
mpf_add(pi,pi,term);
//mpf_out_str(stdout,10,0,term);
//printf("\n");
}
mpf_sqrt_ui(tmp,10005);
mpf_mul_ui(tmp,tmp,426880);
mpf_div(pi,tmp,pi);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
}
void doubFactz(unsigned long long x){
if (x==1||!x)
return;
mpz_mul_ui(z,z,x);
doubFactz(x-2);
}
void newton(unsigned long long times){
mpf_t pi, term, tmp;
mpf_init2(pi,16384);
mpf_init2(term,16384);
mpf_init2(tmp,16384);
mpz_init(z);
mpf_set_ui(pi,0);
long long a;
for (a=0; a<times; a++){
//printf("started\n");
mpz_set_ui(z,1);
factz(a);
mpf_set_z(term,z);
mpz_set_ui(z,1);
doubFactz(2*a+1);
mpf_set_z(tmp,z);
mpf_div(term,term,tmp);
mpf_add(pi,pi,term);
//printf("ended\n");
//mpf_out_str(stdout,10,0,term);
//printf("\n");
}
mpf_mul_ui(pi,pi,2);
mpf_out_str(stdout, 10, 0, pi);
printf("\n");
}