1 (* Pi digits computed with the sreaming algorithm given on pages 4, 6
2 & 7 of "Unbounded Spigot Algorithms for the Digits of Pi", Jeremy
3 Gibbons, August 2004. *)
8 let ( !$ ) = Big_int.big_int_of_int
9 and ( +$ ) = Big_int.add_big_int
10 and ( *$ ) = Big_int.mult_big_int
11 and ( =$ ) = Big_int.eq_big_int
14 let zero = Big_int.zero_big_int
15 and one = Big_int.unit_big_int
22 (* Linear Fractional (aka M=F6bius) Transformations *)
25 let floor_ev (q, r, s, t) x = div_big_int (q *$ x +$ r) (s *$ x +$ t);;
27 let unit = (one, zero, zero, one);;
29 let comp (q, r, s, t) (q', r', s', t') =
30 (q *$ q' +$ r *$ s', q *$ r' +$ r *$ t',
31 s *$ q' +$ t *$ s', s *$ r' +$ t *$ t')
37 let next z = LFT.floor_ev z three
38 and safe z n = (n =$ LFT.floor_ev z four)
39 and prod z n = LFT.comp (ten, neg_ten *$ n, zero, one) z
41 let den = 2 * k + 1 in
42 LFT.comp z (!$ k, !$(2 * den), zero, !$ den)
45 let rec digit k z n row col =
51 printf "\t:%i\n%s" row (string_of_big_int y);
52 digit k (prod z y) (n - 1) row 1
55 print_string(string_of_big_int y);
56 digit k (prod z y) (n - 1) row (col + 1)
58 else digit (k + 1) (cons z k) n row col
60 printf "%*s\t:%i\n" (10 - col) "" (row + col)
63 let digits n = digit 1 LFT.unit n 0 0
67 prerr_endline "Usage: pi_big_int <number of digits to compute for pi>";
72 let args = Sys.argv in
73 if Array.length args <> 2 then usage () else
74 digits (int_of_string Sys.argv.(1))