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
|
program pi;
(* file exemples/pascal/pi.p: compute some digits of pi
+-----------------------------------------------------------------------+
| Copyright 2005-2006, Michel Quercia (michel.quercia@prepas.org) |
| |
| This file is part of Numerix. Numerix is free software; you can |
| redistribute it and/or modify it under the terms of the GNU Lesser |
| General Public License as published by the Free Software Foundation; |
| either version 2.1 of the License, or (at your option) any later |
| version. |
| |
| The Numerix Library 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 |
| Lesser General Public License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public |
| License along with the GNU MP Library; see the file COPYING. If not, |
| write to the Free Software Foundation, Inc., 59 Temple Place - |
| Suite 330, Boston, MA 02111-1307, USA. |
+-----------------------------------------------------------------------+
| |
| Calcul de Pi, formule de Ramanujan |
| |
+-----------------------------------------------------------------------*)
(* cf. "The Caml Numbers Reference Manual", Inria, RT-0141 *)
(* annexe A, pp. 115 et suivantes. *)
uses _name_;
{$ifdef __GPC__}{$X+}{$endif}
(* +--------------------------+
| Sommation dichotomique |
+--------------------------+ *)
const maxprof = 32; (* profondeur de rcursion maximale *)
procedure somme(prec:int; var num,den:xint);
var etapes : int; (* nombre de termes calculer *)
pile : array[0..3*maxprof-1] of xint; (* pile de rcusrion *)
sp : int; (* pointeur de pile *)
i,j : int;
c,p,alpha,beta,gamma,delta,eps,t,u,x : xint;
(* constantes *)
const a = 13591409;
const b = 545140134;
begin
c := of_string('10939058860032000');
p := of_int(0); (* index srie *)
alpha := of_int(1); (* 2p + 1 *)
beta := of_int(1); (* 6p + 1 *)
gamma := of_int(5); (* 6p + 5 *)
delta := of_int(53360); (* c*p^3 *)
eps := of_int(a); (* a + bp *)
t := xnew; (* scratch *)
u := xnew; (* scratch *)
etapes := (prec+197) div 94;
sp := 0;
(* initialise la pile *)
for i := 0 to 3*maxprof-1 do pile[i] := xnew;
for i:=1 to etapes do begin
(* calcule et retranche les termes de rangs p et p+1 *)
mul (t, alpha, beta);
mul (pile[sp], t, gamma);
copy (pile[sp+1], delta);
copy (pile[sp+2], eps);
add_1(p, p, 1);
add_1(alpha, alpha, 2);
add_1(beta, beta, 6);
add_1(gamma, gamma, 6);
sqr (t, p);
mul (u, c, p);
mul (delta, t, u);
add_1(eps, eps, b);
mul (t, delta, pile[sp+2]);
mul (u, pile[sp], eps);
sub (pile[sp+2], t, u);
mul (t, alpha, beta);
mul (u, pile[sp], gamma);
mul (pile[sp], t, u);
mul (pile[sp+1], pile[sp+1], delta);
add_1(p, p, 1);
add_1(alpha, alpha, 2);
add_1(beta, beta, 6);
add_1(gamma, gamma, 6);
sqr (t, p);
mul (u, c, p);
mul (delta, t, u);
add_1(eps, eps, b);
sp := sp+3;
(* combine avec les calculs prcdents *)
j:=1; while (j and i) = 0 do begin
sp := sp - 3;
mul(t, pile[sp+1], pile[sp-1]);
mul(pile[sp-1], pile[sp-3], pile[sp+2]);
add(pile[sp-1], pile[sp-1], t);
mul(pile[sp-3], pile[sp-3], pile[sp]);
mul(pile[sp-2], pile[sp-2], pile[sp+1]);
j := 2*j;
end;
end;
(* termine les calculs en instance *)
sp := sp - 3;
while sp <> 0 do begin
sp := sp - 3;
mul(t, pile[sp+4], pile[sp+2]);
mul(pile[sp+2], pile[sp], pile[sp+5]);
add(pile[sp+2], pile[sp+2], t);
mul(pile[sp+1], pile[sp+1], pile[sp+4]);
end;
(* nettoie les variables locales et retourne la fraction *)
x := num; num := pile[1]; pile[1] := x;
x := den; den := pile[2]; pile[2] := x;
for i := 0 to 3*maxprof-1 do xfree(pile[i]);
xfree(c);
xfree(p);
xfree(alpha);
xfree(beta);
xfree(gamma);
xfree(delta);
xfree(eps);
xfree(t);
xfree(u);
end;
(* +--------------------------------------+
| Calcule pi avec digits+2 dcimales |
+--------------------------------------+ *)
procedure calc_pi(digits:int; pgcd,print,skip,debug,test:boolean; true_pi:pchar);
var prec, i,j,l,l1,l2 : int;
num,den,t,x1,x2,x3 : xint;
s : string[80];
ss : pchar;
begin
if debug then chrono('start');
num := xnew;
den := xnew;
t := xnew;
x1 := xnew;
x2 := xnew;
x3 := xnew;
(* t <- 5^(digits+2) *)
copy_int(t, 5);
power (t, t, digits+2);
if debug then chrono('puiss-5');
(* t <- floor( sqrt(640320) * 10^(digits+2) ) *)
prec := nbits(t) + digits;
sqr (t, t);
mul_1 (t, t, 640320);
shiftl(t, t, 2*digits+4);
sqrt (t, t);
if debug then chrono('sqrt');
(* num/den <- somme de la srie env. 10^(-digits-2) prs *)
somme(prec,num,den);
if debug then begin
str(nbits(num),s);
s := 'series lb=' + s + #0;
chrono(@s[1]);
end;
(* simplifie la fraction si demand (ceci ne vaut pas le coup, le
temps de calcul du pgcd est trs suprieur au temps de calcul
de la division sans simplification) *)
if pgcd then begin
cfrac(x1,x2,x3,num,den,num,den);
if debug then begin
str(nbits(num),s);
s := 'gcd lb=' + s + #0;
chrono(@s[1]);
end;
end;
(* t <- sqrt(640320)*num/den * 10^digits+2) *)
mul (t, num, t);
quo (t, t, den);
if debug then chrono('quotient');
(* on n'a plus besoin de num,den *)
xfree(num);
xfree(den);
(* conversion en dcimal *)
if print then begin
ss := string_of(t);
l := strlen(ss);
if debug then chrono('conversion');
writeln(ss[0],'.');
i := 1;
while ss[i] <> #0 do begin
write(ss[i]);
if (i mod 250) = 0 then begin writeln; writeln; end
else if (i mod 50) = 0 then writeln
else if (i mod 10) = 0 then write(' ')
else if (i mod 5) = 0 then write(' ');
if skip and ((i mod 50) = 0) then begin
j := (l-i) div 50 - 1;
if j > 0 then begin
writeln('... (',j,' lines omitted)');
i := i + 50*j;
end;
end;
i := i+1;
end;
if (i mod 50) <> 1 then writeln;
strfree(ss);
end
else if test then begin
ss := string_of(t);
l1 := strlen(ss);
l2 := strlen(true_pi);
if l1 < l2 then l := l1 else l := l2;
if strncmp(ss,true_pi,l) <> 0
then writeln('error in the ',paramstr(0),' test')
else writeln(paramstr(0),#9'test ok');
strfree(ss);
end;
(* termin *)
xfree(t);
xfree(x1);
xfree(x2);
xfree(x3);
end;
(* +-----------------------+
| Programme principal |
+-----------------------+ *)
var digits,i : int;
pgcd,print,skip,debug,help,test : boolean;
true_pi : pchar;
c : word;
begin
(* les 1000 premiers chiffres de pi pour contrle *)
true_pi := '31415926535897932384626433832795028841971693993751'
+ '05820974944592307816406286208998628034825342117067'
+ '98214808651328230664709384460955058223172535940812'
+ '84811174502841027019385211055596446229489549303819'
+ '64428810975665933446128475648233786783165271201909'
+ '14564856692346034861045432664821339360726024914127'
+ '37245870066063155881748815209209628292540917153643'
+ '67892590360011330530548820466521384146951941511609'
+ '43305727036575959195309218611738193261179310511854'
+ '80744623799627495673518857527248912279381830119491'
+ '29833673362440656643086021394946395224737190702179'
+ '86094370277053921717629317675238467481846766940513'
+ '20005681271452635608277857713427577896091736371787'
+ '21468440901224953430146549585371050792279689258923'
+ '54201995611212902196086403441815981362977477130996'
+ '05187072113499999983729780499510597317328160963185'
+ '95024459455346908302642522308253344685035261931188'
+ '17101000313783875288658753320838142061717766914730'
+ '35982534904287554687311595628638823537875937519577'
+ '81857780532171226806613001927876611195909216420198';
digits := 100;
pgcd:=false; print:=true; skip:=false; debug:=false; help:=false; test:=false;
for i:=1 to paramcount do begin
if paramstr(i) = '-h' then help := true
else if paramstr(i) = '-d' then debug := true
else if paramstr(i) = '-noprint' then print := false
else if paramstr(i) = '-skip' then skip := true
else if paramstr(i) = '-gcd' then pgcd := true
else if paramstr(i) = '-test' then begin
digits := 1000;
print := false;
skip := false;
debug := false;
pgcd := false;
test := true;
end
else begin
val(paramstr(i),digits,c);
c := c; (* to avoid a FPC warning about no-use of c *)
end
end;
if help then writeln('usage: ',paramstr(0),' [digits] [-d] [-noprint] [-skip] [-gcd]')
else calc_pi(digits-2,pgcd,print,skip,debug,test,true_pi);
end.
|