>
> On Tue, Oct 1, 2013 at 11:22 AM, David Chambers <
david.ch...@gmail.com <javascript:>> wrote:
>
> Last night I attempted Project Euler #5 <
http://projecteuler.net/problem=5> in Clojure. The problem is as follows:
>
> > # Smallest multiple
> >
> > 2520 is the smallest number that can be divided by each of the numbers
> > from 1 to 10 without any remainder.
> >
> > What is the smallest positive number that is evenly divisible by all of the
> > numbers from 1 to 20?
>
> Here's my solution:
>
> (defn divisible?
> [numer denom]
> (= 0 (mod numer denom)))
>
> (defn smallest-multiple
> "Find the smallest positive number that is evenly divisible by all
> of the numbers from 1 to n."
> [n]
> (let [s (range 1 (inc n))]
> (first (filter (fn [x] (every? (partial divisible? x) s))
> (rest (range))))))
>
> This gives the expected answer for /n/ of 10, but takes a long time to
> solve /n/ of 20. I'd like to understand what make this code slow. Am I
> doing something inefficiently? What steps might I take to rework this
> code to have it solve /n/ of 20 within a minute?
>
I'm always struck by how attractive brute force solutions look in clojure (and lisps in general I'd say). Very concise
and plain about what it is trying to do. I'm not sure if that is a good or bad thing about the language.
StanD.