Created
November 14, 2012 20:32
-
-
Save lispm/4074589 to your computer and use it in GitHub Desktop.
FACTORS in Common Lisp using ITERATE
This file contains 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
(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