# HG changeset patch # User Steve Losh # Date 1488044551 0 # Node ID 51bb5533cd821c68880d3c086674a96dc97cd094 # Parent d5875ad65300af4fe09e391e85ef61b5f28904ce Add pentagonal helpers diff -r d5875ad65300 -r 51bb5533cd82 src/euler.lisp --- a/src/euler.lisp Sat Feb 25 17:20:04 2017 +0000 +++ b/src/euler.lisp Sat Feb 25 17:42:31 2017 +0000 @@ -28,11 +28,9 @@ (defun digits-to-number (digits) - (iterate (with result = 0) - (for d :in-whatever digits) - (mulf result 10) - (incf result d) - (finally (return result)))) + (reduce (lambda (total digit) + (+ (* total 10) digit)) + digits)) (defun palindromep (n &optional (radix 10)) @@ -297,6 +295,15 @@ (squarep x)))) +(defun pentagon (n) + (* n (- (* 3 n) 1) 1/2)) + +(defun pentagonp (n) + ;; We can ignore the - branch of the quadratic equation because negative + ;; numbers aren't indexes. + (dividesp (+ 1 (sqrt (1+ (* 24 n)))) 6)) + + (defun parse-strings-file (filename) (-<> filename read-file-into-string