Skip to content

Commit

Permalink
Solved the first problem using common lisp.
Browse files Browse the repository at this point in the history
  • Loading branch information
nixeagle committed Feb 19, 2010
1 parent 75e4619 commit 94a0ece
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 1/sum-integers.lisp
@@ -0,0 +1,23 @@
(in-package :nisp.euler)

(defun sum-integers-if (predicate start end &optional (step 1))
"Sum numbers satisfying PREDICATE from START to END."
(iterate (for n :from start :to end :by step)
(when (funcall predicate n)
(sum n :into result))
(declare (fixnum result)) ; Help the compiler optimize.
(finally (return result))))

(defun solution-1/1 ()
"Call `sum-integers-if' after we compose predicate.
This is a purely functional way of solving this problem."
(sum-integers-if (disjoin (compose (curry #'= 0) (rcurry #'mod 3))
(compose (curry #'= 0) (rcurry #'mod 5)))
0 999))

(defun solution-1/2 ()
"Use a local function definition for the predicate."
(flet ((validp (n)
(or (= 0 (mod n 3)) (= 0 (mod n 5)))))
(sum-integers-if #'validp 0 999)))
7 changes: 7 additions & 0 deletions nisp.euler.asd
@@ -0,0 +1,7 @@
(asdf:defsystem :nisp.euler
:serial t
:components
((:file "package")
(:module "1"
:components
((:file "sum-integers")))))
3 changes: 3 additions & 0 deletions package.lisp
@@ -0,0 +1,3 @@
(defpackage #:nisp.euler
(:use :cl :iterate :alexandria)
(:nicknames :euler))

0 comments on commit 94a0ece

Please sign in to comment.