Last active
December 29, 2015 02:49
-
-
Save cdemyanovich/7603158 to your computer and use it in GitHub Desktop.
Solutions to 3 Project Euler problems for Clojure 101 at 8th Light.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns euler.core) | |
(defn multiple-of-3-or-5? [n] (or (= 0 (mod n 3)) (= 0 (mod n 5)))) | |
(defn sum-of-multiples-of-3-or-5-below [n] | |
(apply + (filter multiple-of-3-or-5? (range 1 n)))) | |
(defn fibs [] | |
(map first (iterate (fn [[a b]] [b (+ a b)]) [1N 1N]))) | |
(defn sum-of-even-fibonaccis-less-than [n] | |
(apply + (filter even? (take-while #(< % n) (fibs))))) | |
(defn first-fibonacci-term-of-length-digits [n] | |
; (first (drop-while #(< (count (str %)) n) (fibs)))) | |
(let [[shorter-terms longer-terms] (split-with #(< (count (str %)) n) (fibs))] | |
(+ 1 (count shorter-terms)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns euler.core-spec | |
(:require [speclj.core :refer :all] | |
[euler.core :refer :all])) | |
(describe "sum of multiples of 3 and 5 below 1000" | |
(it "is 233168" | |
(should= 233168 (sum-of-multiples-of-3-or-5-below 1000)))) | |
(describe "sum of even Fibonacci numbers less than 4 million" | |
(it "is 4613732" | |
(should= 4613732 (sum-of-even-fibonaccis-less-than 4000000)))) | |
(describe "first Fibonacci term of length digits" | |
(it "is 4782" | |
(should= 4782 (first-fibonacci-term-of-length-digits 1000))))- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment