DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Adding Natural Numbers
I'm trying to work through Project Euler in Clojure. Maybe that will help me figure Clojure out. I included my test cases, because I think that helps show my thinking about things.
This didn't take me nearly as long as my last example. Hurray for me!
;; Goal -- add all sums <=1000 that are divisible by 3 or 5
;; with more than a little help from:
;; http://clojure.roboloco.net/?p=11
(ns squarepegsystems.naturalsums
(:use clojure.test))
(defn divisible [ little big]
(=(mod big little) 0)
)
(defn multiple-3-or-5 [value]
(or (divisible 5 value)
(divisible 3 value)
)
)
(deftest testdiv
(is (= true (divisible 1 1)))
(is (= true (divisible 1 1)))
(is (= false (divisible 2 3)))
(is (= true (divisible 2 4)))
(is (= false (divisible 4 2)))
)
(deftest testmultiple-3-or-5
(is (= true (multiple-3-or-5 5)))
(is (= true (multiple-3-or-5 15)))
(is (= true (multiple-3-or-5 3)))
(is (= true (multiple-3-or-5 9)))
)
(deftest testfilter
(is(=() (filter #(divisible 5 %) [1 2 3 4])))
(is(= [10 15 20] (filter #(divisible 5 %) [10 15 20])))
)
;(run-tests)
(prn (reduce +(filter #(multiple-3-or-5 %) (range 1000))))





