e7d5fdbc48b4

Problem 45
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 27 Feb 2017 00:26:54 +0000
parents 68bdc54223a6
children 6124d264353e
branches/tags (none)
files src/euler.lisp

Changes

--- a/src/euler.lisp	Sun Feb 26 22:01:32 2017 +0000
+++ b/src/euler.lisp	Mon Feb 27 00:26:54 2017 +0000
@@ -304,6 +304,15 @@
   (dividesp (+ 1 (sqrt (1+ (* 24.0d0 n)))) 6))
 
 
+(defun hexagon (n)
+  (* n (1- (* 2 n))))
+
+(defun hexagonp (n)
+  ;; We can ignore the - branch of the quadratic equation because negative
+  ;; numbers aren't indexes.
+  (dividesp (+ 1 (sqrt (1+ (* 8.0d0 n)))) 4))
+
+
 (defun parse-strings-file (filename)
   (-<> filename
     read-file-into-string
@@ -1343,6 +1352,22 @@
               (setf result distance)))
           (return))))))
 
+(defun problem-45 ()
+  ;; Triangle, pentagonal, and hexagonal numbers are generated by the following
+  ;; formulae:
+  ;;
+  ;; Triangle	 	Tn=n(n+1)/2	 	1, 3, 6, 10, 15, ...
+  ;; Pentagonal	 	Pn=n(3n−1)/2	 	1, 5, 12, 22, 35, ...
+  ;; Hexagonal	 	Hn=n(2n−1)	 	1, 6, 15, 28, 45, ...
+  ;;
+  ;; It can be verified that T285 = P165 = H143 = 40755.
+  ;;
+  ;; Find the next triangle number that is also pentagonal and hexagonal.
+  (iterate
+    (for i :from 286)
+    (for n = (triangle i))
+    (finding n :such-that (and (pentagonp n) (hexagonp n)))))
+
 
 ;;;; Tests --------------------------------------------------------------------
 (def-suite :euler)
@@ -1392,6 +1417,7 @@
 (test p42 (is (= 210 (problem-42))))
 (test p43 (is (= 16695334890 (problem-43))))
 (test p44 (is (= 5482660 (problem-44))))
+(test p45 (is (= 1533776805 (problem-45))))
 
 
 ;; (run! :euler)