Waiting for Singularity

Thoughts on science, tech and other preoccupations

Pi Approximation in Python and Ruby

In 1910, the mathematician Srinivasa Ramanujan found several rapidly converging infinite series that can be used to generate a numerical approximation of pi. His series are now the basis for the fastest algorithms currently used to calculate π.

Below are implementations of a function called estimate_pi that uses his formula to compute and return an estimate of pi (an exercise from Chapter 7 of Think Python by Allen Downey).

Solution in Python

import math

def factorial(n):
    if n == 0:
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        return result

def estimate_pi():
    total = 0
    k = 0
    factor = 2 * math.sqrt(2) / 9801
    keep_going = True
    while keep_going == True:
        numerator = float(factorial(4*k) * (1103 + 26390 * k))
        denominator = float(factorial(k)**4) * 396**(4*k)
        term = factor * float(numerator/denominator)
        total += term
        k += 1
        if term < 1e-15:
            keep_going == False
            print "Finished at iteration number", k
            break
    return 1 / total

print estimate_pi()

Solution in Ruby

def factorial(n)
  if n == 0
    return 1
  else
    recurse = factorial(n-1)
    result = n * recurse
  end
  return result
end

def estimate_pi
  total = 0
  k = 0
  factor = 2 * Math.sqrt(2) / 9801
  keep_going = true

  while keep_going == true
    numerator = (factorial(4*k).to_f * (1103 + 26390 * k).to_f)
    denominator = (factorial(k).to_f**4) * 396**(4*k).to_f
    term = factor.to_f * (numerator.to_f/denominator.to_f)
    total += term
    k += 1
    puts term < 1E-15

    if term < 1E-15
      keep_going == false
      puts "Finished at iteration number #{k}"
      break
    end

  end
  return 1 / total
end

puts estimate_pi()

Outputs:

Finished at iteration number 3
3.14159265359