From: Benoit Daloze Date: 2010-01-15T02:45:50+09:00 Subject: Re: [QUIZ] Digits of e (#226) --0016364d29d1c7bd24047d23747a Content-Type: multipart/alternative; boundary=0016364d29d1c7bd1d047d237478 --0016364d29d1c7bd1d047d237478 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello, 2010/1/8 Daniel Moore > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- > > 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 > > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- > > ## 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) =3D e^x at the point x =3D 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 =CF=80, and > the imaginary unit i. These are the five constants appearing in one > formulation of [Euler's identity][2]. > > This week=E2=80=99s 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) > [2]: 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(s= o 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 ------------------------------------------------------- 2.718281828459045 lim(n->=E2=88=9E) (1+1/n)**n with n =3D 100000000 2.7182817983473577 3.011168736577474e-08 lim(n->0) (1+n)**(1/n) with n =3D 1.0e-08 2.7182817983473577 3.011168736577474e-08 =CE=A3(n=3D0,=E2=88=9E) 1/n! with n =3D 17 2.7182818284590455 -4.440892098500626e-16 lim(n->=E2=88=9E) n/(=E2=88=9A(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) =3D =E2=88=9A(x,x) with p =3D 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 =CF= =80 (hum, the complex part of the result, divided by n), awesome :D (or this is surprising me at least :) ) include Math p E # 2.718281828459045 puts formula =3D -> n { ((2*n+1)/(2*n-1).to_f)**n } puts "lim(n->inf) ((2n+1)/(2n-1))**n with n =3D #{n =3D 100_000}" puts "e" p e =3D formula[n] # 2.718281828493031 p e - E # 3.398570314061544e-11 puts "lim(n->0) ((2n+1)/(2n-1))**n with n =3D #{n =3D 1.0/100_000_000}" puts "=CF=80" p pi =3D formula[n] # (1.0+3.141592653589794e-08i) p pi =3D pi.imaginary / n # 3.1415926535897936 p pi - PI # 4.440892098500626e-16 Cheers, B.D. --0016364d29d1c7bd1d047d237478 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello,

2010/1/8 Daniel Moore <yahivin@gmail.com>
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

The three rules of Ruby Quiz:

1. =C2=A0Please do not post any solutions or spoiler discussion for this quiz until 48 hours have elapsed from the time this message was
sent.

2. =C2=A0Support Ruby Quiz by submitting ideas and responses
as often as you can.

3. =C2=A0Enjoy!

Suggestion: =C2=A0A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. =C2=A0Please reply to the original quiz message, if you can.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

RSS Feed: http://rubyquiz.strd6.com/quizzes.rss

Suggestions?: http://rubyquiz.strd6.com/suggestions

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

## 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) =3D e^x at the point x =3D 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 =CF=80, and the imaginary unit i. These are the five constants appearing in one
formulation of [Euler's identity][2].

This week=E2=80=99s quiz is to write a Ruby program that can compute the fi= rst
100,000 digits of e.

Have fun!

[1]: http://en.wikipedia.org/wiki/E_(mathematical_constant)
[2]:
http://en.wikipedia.org/wiki/Euler's_identity

--
-Daniel
http://rubyquiz.str= d6.com


I got some fun playing with computing e. I di= dn'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(s= o don't exepect to get a ot of precision).

Anyway, we can see the continuous fraction is good, and it's the on= ly one who worked easily with BigDecimal(I got only 113 digits, shame on me= ...).

Here is the output of e.rb
-------------------------------= ------------------------
2.718281828459045

lim(n->=E2=88=9E) (1+1/n)**n with n =3D 1000000= 00
2.7182817983473577
3.011168736577474e-08

lim(n->0) (1+n)= **(1/n) with n =3D 1.0e-08
2.7182817983473577
3.011168736577474e-08
=CE=A3(n=3D0,=E2=88=9E) 1/n! with n =3D 17
2.7182818284590455
-4.440892098500626e-16

lim(n->=E2=88=9E) n/= (=E2=88=9A(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.718= 281828459045
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) =3D =E2=88=9A(x,x) with p =3D 16
2.7182818284590446
4.440892098500626e-16
-------------------------------------------------------

And I got al= so 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 =CF=80 (h= um, the complex part of the result, divided by n), awesome :D (or this is s= urprising me at least :) )

include Math
p E # 2.718281828459045
puts

formula =3D ->= ; n { ((2*n+1)/(2*n-1).to_f)**n }

puts "lim(n->inf) ((2n+1)/= (2n-1))**n with n =3D #{n =3D 100_000}"
puts "e"
p e = =3D formula[n] # 2.718281828493031
p e - E # 3.398570314061544e-11

puts "lim(n->0) ((2n+1)/(2n-= 1))**n with n =3D #{n =3D 1.0/100_000_000}"
puts "=CF=80"=
p pi =3D formula[n] # (1.0+3.141592653589794e-08i)
p pi =3D pi.imagi= nary / n # 3.1415926535897936
p pi - PI # 4.440892098500626e-16

Cheers,

B.D.
--0016364d29d1c7bd1d047d237478-- --0016364d29d1c7bd24047d23747a Content-Type: application/octet-stream; name="e.rb" Content-Disposition: attachment; filename="e.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_g4ftv4hr0 IyBlbmNvZGluZzogdXRmLTgKCiMgdGltZSBydWJ5IC1yIGJpZ2RlY2ltYWwvbWF0aCAtZSAnQmln TWF0aC5FKDEwXzAwMCknID0+IDBtMjEuODMzcwoKaW5jbHVkZSBNYXRoCgpkZWYgzqMoZnJvbSA9 IDAsIHRvKQoJKGZyb20uLnRvKS5pbmplY3QoMCkgeyB8c3VtLCBufCBzdW0gKyB5aWVsZChuKSB9 CmVuZAoKY2xhc3MgSW50ZWdlcgoJZGVmICEKCQkoMS4uc2VsZikuaW5qZWN0IHsgfGYsIG98IGYg KiBvIH0gfHwgMQoJZW5kCmVuZAoKZGVmIGJpbmFyeV9zZWFyY2goZiwgbG93ZXIsIHVwcGVyLCBw KQoJcmFpc2UgJ05vIHJvb3QgZm91bmQnIGlmIGxvd2VyID4gdXBwZXIKCW1pZGRsZSA9IChsb3dl cit1cHBlcikvMi4wCgkjIG0+MCxsPjAgPT4gbS11OyBtPjAsbDwwID0+IGwtbQoJIyBtPDAsbD4w ID0+IGwtbTsgbTwwLGw8MCA9PiBtLXUKCWNhc2UgZlttaWRkbGVdLnJvdW5kKHApKmZbbG93ZXJd IDw9PiAwCgl3aGVuIC0xICMgTmVnYXRpdmUgbTwwLGw+MCBvciBtPjAsbDwwCgkJYmluYXJ5X3Nl YXJjaChmLCBsb3dlciwgbWlkZGxlLCBwKQoJd2hlbiAwICMgPQoJCW1pZGRsZS5yb3VuZChwKQoJ d2hlbiAxICMgUG9zaXRpdmUgbT4wLGw+MCBvciBtPDAsbDwwCgkJYmluYXJ5X3NlYXJjaChmLCBt aWRkbGUsIHVwcGVyLCBwKQoJZW5kCmVuZ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