Created
October 11, 2016 20:27
-
-
Save sleibrock/cf6618ddbb86deb0ad6af6b5390c2846 to your computer and use it in GitHub Desktop.
Approximate e^x
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
#lang racket | |
;; Compute e^x with an approximation | |
;; 1 + x + x^2/2! + x^3/3! + ... up to ten terms | |
;; No Defines were hurt in the making of this program | |
(for-each | |
(compose | |
displayln | |
(λ (s) (apply (λ (a b) (string-append a "." (substring b 0 (if (< (string-length b) 4) (string-length b) 4)))) (string-split s "."))) | |
number->string | |
exact->inexact | |
(λ (x) (+ 1 x (foldl + 0 (map (λ (p) (/ (expt x p) (foldl * 1 (cdr (range (add1 p)))))) (cdr (cdr (range 10)))))))) | |
(for/list ([x (in-range (read))]) (read))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment