digits_of_pi :: [num]
  digits_of_pi = (concat . map (next_batch.(2^))) [0..]
                 where
                 next_batch n = drop (n div 2) (n_pi_digits n)


n_pi_digits :: num -> [num]
|| computes pi truncated [rounded down] to n base-10000 digits
  n_pi_digits n
    = normalize (do_squirtings (rep (14*n-1) 2000) 2000)


do_squirtings :: [num] -> num -> [num]
  do_squirtings array digit
    = digit : do_squirtings (drop 14 array') digit', if array~=[]
    = [digit div 10000 * 10000], otherwise    
      where
      (array',digit') = do_carries (map (*10000) array) (#array)
      do_carries (a:b:rest) i
        = ((a mod (2*i+1)):array_so_far, digit_so_far)
          where
          (array_so_far, digit_so_far) = do_carries
                                          (b+a div (2*i+1) * i : rest) (i-1)
      do_carries [a] i = ([a mod 3], a div 3)


normalize :: [num] -> [num]
  normalize [] = []
  normalize (h:t)
    = buffer_up h 0 t
      where
      buffer_up b c [] = []
      buffer_up b c (h:t)
        = buffer_up b (c+1) t, if h=9999
        = b:rep c 9999++buffer_up h 0 t, if h<=9998
        = (b+h div 10000):rep c 0++buffer_up (h mod 10000) 0 t, otherwise
