Skip to content

Instantly share code, notes, and snippets.

@Nooby
Last active December 22, 2015 06:28
Show Gist options
  • Save Nooby/6430941 to your computer and use it in GitHub Desktop.
Save Nooby/6430941 to your computer and use it in GitHub Desktop.
Some useful math functions for lisp.
(defun cube-root (n)
;; Calculates the Cube Root of n.
(expt n 1/3))
(defun factors (n &aux (lows '()) (highs '()))
;; We iterate in the range 1..sqrt(n) collecting ‘low’ factors and corresponding ‘high’ factors,
;; and combine at the end to produce an ordered list of factors.
;; http://rosettacode.org/wiki/Factors_of_an_integer#Common_Lisp
(do ((limit (isqrt n)) (factor 1 (1+ factor)))
((= factor limit)
(when (= n (* limit limit))
(push limit highs))
(nreconc lows highs))
(multiple-value-bind (quotient remainder) (floor n factor)
(when (zerop remainder)
(push factor lows)
(push quotient highs)))))
(defun primep (n)
;; Tests if the numbers is a Prime.
(= 2 (length (factors n))))
(defun slope (first-point last-point)
;; Calculates the slope between 2 Points
(let ((change_x (float (- (pop last-point) (pop first-point))))
(change_y (float (- (pop last-point) (pop first-point)))))
(float (/ change_y change_x))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment