51bb5533cd82

Add pentagonal helpers
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 25 Feb 2017 17:42:31 +0000
parents d5875ad65300
children 68bdc54223a6
branches/tags (none)
files src/euler.lisp

Changes

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