Created
May 10, 2013 19:57
-
-
Save PuercoPop/5556962 to your computer and use it in GitHub Desktop.
2: 1978: The year 1978 is such that the sum of the first two digits and the latter two digits is equal to the middle two digits, i.e. 19 + 78 = 97. What is the next year (after 1978) for which this is true?
This file contains hidden or 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 validate-candidate (candidate) | |
(let ((candidate (write-to-string candidate))) | |
(unless (= (length candidate) 4) | |
(error "Test valid only for 4 digit numbers")) | |
(let ((initial-digits (parse-integer (subseq candidate 0 2))) | |
(middle-digits (parse-integer (subseq candidate 1 3))) | |
(final-digits (parse-integer (subseq candidate 2 4)))) | |
(if (= middle-digits (+ initial-digits final-digits)) | |
t | |
nil)))) | |
(loop with val = 1979 | |
do (format t "~a~%" (incf val)) | |
until (validate-candidate val)) | |
;; Answer 2307 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment