Created
June 4, 2013 16:09
-
-
Save furushchev/5707204 to your computer and use it in GitHub Desktop.
Project Euler Problem12
Fast
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 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