Created
September 15, 2018 22:26
-
-
Save realeroberto/a68f61a59e1653c07df90ded1446d5f5 to your computer and use it in GitHub Desktop.
Approximate the value of e using the power series expansion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/tclsh | |
# | |
# approximate e using the power series expansion | |
# | |
proc approx_e {{n 20}} { | |
set value 1 | |
set factorial 1 | |
set i 1 | |
while {$i <= $n} { | |
set factorial [expr {$factorial * $i}] | |
set value [expr {$value + 1.0/$factorial}] | |
incr i | |
} | |
return $value | |
} | |
puts [approx_e] | |
# ex: ts=4 sw=4 et filetype=tcl noexpandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment