
Create a program to continually calculate and output the next decimal digit of   



π


{\displaystyle \pi }

   (pi).

The program should continue forever (until it is aborted by the user) calculating and outputting each decimal digit in succession.

The output should be a decimal sequence beginning   3.14159265 ...



Note: this task is about   calculating   pi.   For information on built-in pi constants see Real constants and functions.



Related Task Arithmetic-geometric mean/Calculate Pi

require "big"
 
def pi
  q, r, t, k, n, l = [1, 0, 1, 1, 3, 3].map { |n| BigInt.new(n) }
  dot_written = false
  loop do
    if 4*q + r - t < n*t
      yield n
      unless dot_written
        yield '.'
        dot_written = true
      end
      nr = 10*(r - n*t)
      n = ((10*(3*q + r)) / t) - 10*n
      q *= 10
      r = nr
    else
      nr = (2*q + r) * l
      nn = (q*(7*k + 2) + r*l) / (t*l)
      q *= k
      t *= l
      l += 2
      k += 1
      n = nn
      r = nr
    end
  end
end
 
pi { |digit_or_dot| print digit_or_dot; STDOUT.flush }
 