Created
October 28, 2014 19:59
-
-
Save trendsetter37/c892f5e95beaee31460e to your computer and use it in GitHub Desktop.
Evaluate e^x HackerRank Common Lisp implementation
This file contains hidden or 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
| (read) ;; Throwing this away. Not needed | |
| (defun read-list () | |
| "reading in list of number" | |
| (let ((n (read *standard-input* nil))) | |
| (if (null n) | |
| nil | |
| (cons n (read-list))))) | |
| (defun factorial! (number) | |
| "O(n)? naive but works: returns the factorial of the number given" | |
| (loop for x from 1 upto number | |
| for y = 1 then (* x y) | |
| finally (return y))) | |
| (defun series-expansion (number) | |
| "Evaluates e^x taking the first ten terms from the series expansion method" | |
| (+ (reduce #'+ (loop for i from 1 upto 9 collect (/ (float (expt number i)) (factorial! i)))) 1)) | |
| (format t "~{~d~%~}" (map 'list #'series-expansion (read-list))) ;; print the results |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not efficient. will revisit later.