Skip to content

Instantly share code, notes, and snippets.

@sleibrock
Created October 11, 2016 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleibrock/cf6618ddbb86deb0ad6af6b5390c2846 to your computer and use it in GitHub Desktop.
Save sleibrock/cf6618ddbb86deb0ad6af6b5390c2846 to your computer and use it in GitHub Desktop.
Approximate e^x
#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