Skip to content

Instantly share code, notes, and snippets.

@lispm
Created November 14, 2012 20:32
Show Gist options
  • Save lispm/4074589 to your computer and use it in GitHub Desktop.
Save lispm/4074589 to your computer and use it in GitHub Desktop.
FACTORS in Common Lisp using ITERATE
(defun factors (n)
(iterate
(with limit = (isqrt n))
(for factor from 1 below limit)
(for (values q r) = (floor n factor))
(when (zerop r)
(collect factor into lows)
(collect q into highs))
(finally (setf highs (reverse highs))
(when (= n (* limit limit))
(push limit highs))
(return (nconc lows highs)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment