Skip to content

Instantly share code, notes, and snippets.

@g000001
Created February 19, 2020 16:32
Show Gist options
  • Save g000001/706539a7c63f221fd4798193398e35cf to your computer and use it in GitHub Desktop.
Save g000001/706539a7c63f221fd4798193398e35cf to your computer and use it in GitHub Desktop.
prog style
;; a la MACLISP
(DEFUN FACTORS (N)
(PROG (FACS SQRTN D)
(SETQ FACS () )
(SETQ D 2)
(GO L1)
L2 (SETQ N (FLOOR N D))
(SETQ FACS (CONS D FACS))
L1 (SETQ SQRTN (ISQRT N))
L (COND ((> D SQRTN) (GO X)))
(COND ((ZEROP (MOD N D)) (GO L2)))
(SETQ D (1+ D))
(GO L)
X (COND ((= 1 N) (RETURN FACS)))
(SETQ FACS (CONS N FACS))
(RETURN FACS)))
;; a la Common Lisp
(defun factors (n)
(prog ((facs '())
(sqrtn (isqrt n))
(d 2))
(declare (integer sqrtn d)
(list facs))
(go L)
L1 (setq n (floor n d))
(push d facs)
(setq sqrtn (isqrt n))
L (when (> d sqrtn) (go X))
(when (zerop (mod n d)) (go L1))
(incf d)
(go L)
X (when (= 1 n) (return facs))
(push n facs)
(return facs)))
(factors 9999999999)
→ (9091 271 41 11 3 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment