File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed
Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -74,8 +74,9 @@ For more usage examples check the
7474Some interesting examples there:
7575
7676* [ factorial.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/factorial.lua ) - calculate factorial of 100
77- * [ fibonnaci.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/fibonnaci.lua ) - calculate the 1001th fibonacci number
78- * [ pi.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/pi.lua ) - calculate the first 100 pi digits
77+ * [ fibonnaci.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/fibonnaci.lua ) - calculate the 1001th number of the Fibonacci sequence
78+ * [ pi.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/pi.lua ) - calculate the first 100 digits of Pi
79+ * [ e.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/e.lua ) - calculate the first 100 digits of Euler's number
7980* [ rsa.lua] ( https://github.com/edubart/lua-bint/blob/master/examples/rsa.lua ) - simple RSA example for encrypting/decrypting messages
8081
8182## Tests
Original file line number Diff line number Diff line change 1+ -- Compute the first 100 digits of Euler's number
2+ -- See https://en.wikipedia.org/wiki/E_(mathematical_constant)
3+
4+ local bint = require ' bint'
5+
6+ bint .scale (512 )
7+
8+ local digits = 100
9+ local e = bint .zero ()
10+ local one = bint .ipow (10 , digits + 10 )
11+ local factorial = bint .one ()
12+ for i = 0 ,digits + 10 do
13+ e = e + (one // factorial )
14+ factorial = factorial * (i + 1 )
15+ end
16+ e = tostring (e )
17+ e = e :sub (1 ,1 ) .. ' .' .. e :sub (2 , digits + 1 )
18+ print (e )
19+ assert (e == ' 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274' )
You can’t perform that action at this time.
0 commit comments