Hello,
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.
2. Support Ruby Quiz by submitting ideas and responses
as often as you can.
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RSS Feed: http://rubyquiz.strd6.com/quizzes.rss
Suggestions?: http://rubyquiz.strd6.com/suggestions
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Digits of e (#226)
Wayumbe Rubyists,
The mathematical constant e is the unique real number such that the
value of the derivative (slope of the tangent line) of the function
f(x) = e^x at the point x = 0 is exactly 1. The function e^x so
defined is called the exponential function, and its inverse is the
natural logarithm, or logarithm to base e.[1]
e is one of the most important numbers in mathematics, alongside the
additive and multiplicative identities 0 and 1, the constant π, and
the imaginary unit i. These are the five constants appearing in one
formulation of [Euler's identity][2].
This week’s quiz is to write a Ruby program that can compute the first
100,000 digits of e.
Have fun!
[1]: http://en.wikipedia.org/wiki/E_(mathematical_constant)<http://en.wikipedia.org/wiki/E_(mathematical_constant)>
[2]: http://en.wikipedia.org/wiki/Euler’s_identity<http://en.wikipedia.org/wiki/Euler’s_identity>
--
-Daniel
http://rubyquiz.strd6.com
I got some fun playing with computing e. I didn't get any formal solution
(it definitely takes too long ...)
So, e.rb contains 8 basic methods to compute e, using basic Float of ruby(so
don't exepect to get a ot of precision).
Anyway, we can see the continuous fraction is good, and it's the only one
who worked easily with BigDecimal(I got only 113 digits, shame on me ...).
Here is the output of e.rb
e.rb (1.99 KB)
···
2010/1/8 Daniel Moore <yahivin@gmail.com>
-------------------------------------------------------
2.718281828459045
lim(n->∞) (1+1/n)**n with n = 100000000
2.7182817983473577
3.011168736577474e-08
lim(n->0) (1+n)**(1/n) with n = 1.0e-08
2.7182817983473577
3.011168736577474e-08
Σ(n=0,∞) 1/n! with n = 17
2.7182818284590455
-4.440892098500626e-16
lim(n->∞) n/(√(n,n!)) with n : 170
2.663087878748024
0.055193949711020984
[[2;1,2,1,1,4,1,1,6,1,1,8,1,1,...,2n,1,1,...]] with 23 numbers
2.718281828459045
0.0
[[1,0,1,1,2,1,1,4,1,1,6,1,1,8,1,1,...]] with 25 numbers
2.718281828459045
0.0
[[1,0.5,12,5,28,9,44,13,60,17,...,4(4n-1),4n+1,...]] with 10 numbers
2.718281828459045
0.0
Global maximum of f(x) = √(x,x) with p = 16
2.7182818284590446
4.440892098500626e-16
-------------------------------------------------------
And I got also a strange thing. I saw somewhere it's possible to compute e
with lim(n->inf) ((2n+1)/(2n-1))**n, but changing the limit to 0 gives π
(hum, the complex part of the result, divided by n), awesome
(or this is
surprising me at least
)
include Math
p E # 2.718281828459045
puts
formula = -> n { ((2*n+1)/(2*n-1).to_f)**n }
puts "lim(n->inf) ((2n+1)/(2n-1))**n with n = #{n = 100_000}"
puts "e"
p e = formula[n] # 2.718281828493031
p e - E # 3.398570314061544e-11
puts "lim(n->0) ((2n+1)/(2n-1))**n with n = #{n = 1.0/100_000_000}"
puts "π"
p pi = formula[n] # (1.0+3.141592653589794e-08i)
p pi = pi.imaginary / n # 3.1415926535897936
p pi - PI # 4.440892098500626e-16
Cheers,
B.D.