Skip to content

Instantly share code, notes, and snippets.

@trendsetter37
Created October 28, 2014 19:59
Show Gist options
  • Select an option

  • Save trendsetter37/c892f5e95beaee31460e to your computer and use it in GitHub Desktop.

Select an option

Save trendsetter37/c892f5e95beaee31460e to your computer and use it in GitHub Desktop.
Evaluate e^x HackerRank Common Lisp implementation
(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
@trendsetter37

Copy link
Copy Markdown
Author

Not efficient. will revisit later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment