Skip to content

Instantly share code, notes, and snippets.

@furushchev
Created June 4, 2013 16:09
Show Gist options
  • Save furushchev/5707204 to your computer and use it in GitHub Desktop.
Save furushchev/5707204 to your computer and use it in GitHub Desktop.
Project Euler Problem12 Fast
(defun sum-n (n)
"(+ 1 2 3 ... n)"
(* n (1+ n) 1/2))
(defun check-div (num)
"return number of factors of num"
(labels ((check-div-r (n k)
(if (= k 1)
1
(if (integerp (/ n k))
(1+ (check-div-r n (1- k)))
(check-div-r n (1- k))))))
(multiple-value-bind (int-unused f) (round (sqrt num))
(if (zerop f)
(1+ (* 2 (check-div-r num (floor (sqrt num)))))
(* 2 (check-div-r num (floor (sqrt num)))))
)))
(defun prob-12 (num)
(do ((i 1 (1+ i)) (result 0))
((> result num) (format t "ans: ~A~%" (sum-n (1- i))))
(setq result (check-div (sum-n i)))
))
(time (prob-12 500))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment