Skip to content

Commit 4d772fb

Browse files
committed
Add euler's number example
1 parent 3fcee7f commit 4d772fb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ For more usage examples check the
7474
Some 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

examples/e.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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')

0 commit comments

Comments
 (0)