Calculating the natural logarithm value with Ruby

By Alvin J. Alexander, devdaily.com

Still reading Calculus Made Easy, they note that 'e' (the natural logarithm) is the limit of the following series:

1 + 1/1! + 1/2! + 1/3! ...

To test this I created the following Ruby program.

# a program to calculate 'e' using ruby.
# 'e' is the limit of this series:
# 1 + 1/1! + 1/2! + 1/3! ...

# determine the factorial value of a given number
def factorial(num)
  total=1
  for i in (1..num)
    total = total * i
  end
  total
end

e = 1.0
for den in (1..20)
  e = e + 1.0/factorial(den)
end

puts "e = #{e}"

devdaily logo