Project Euler in Clojure – Problem 5

In trying to learn Clojure and wrap my head around good functional programming, and hoping to learn more idiomatic Clojure, I have started working through the Project Euler problems. In doing this, I have also setup a repository on github.com to keep track of my progress, which can be found at https://github.com/stevenproctor/project-euler-clojure.  My approach to Problem 4 can be found here.

Problem 5 of Project Euler is described as:

What is the smallest positive number that is evenly
divisible by all of the numbers from 1 to 20?

In reading this problem I realized that this is just another way to state:

Find the Least Common Multiple of all of the numbers between 1 and 20.

The solution I came up with is:

(ns project-euler.core
  (:require [clojure.string :as string]
            clojure.math.numeric-tower))

(defn problem5
  ([] (problem5 20))
  ([n] (reduce lcm 1 (range 2 (inc n)))))

I found that previous to Clojure 1.2 there was a math library in Clojure-Contrib, which after 1.2 is now been moved to clojure.math.numeric-tower, so that now gets included in the vector of libraries in the :require of the ns function.

Once I had this, it was now just as simple as calling the reduce function with the function lcm, feeding it a initialization value of 1 to the reduce function and having that operate on the range of numbers from 2 to the number bound to the var n, which when called without parameters will use the value of twenty as specified in the problem definition. By defining the different arities for the function problem5, it allowed me to call the function with different numbers to the results of the function, as the full description gave the solution for the numbers in the range of 1 through 10.

Again, I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.

**Update**
My solution to Problem 6 has been posted here.

–Proctor

3 thoughts on “Project Euler in Clojure – Problem 5

  1. Pingback: Project Euler in Clojure – Problem 4 « Proctor It

  2. Pingback: Project Euler in Clojure – Problem 6 « Proctor It

  3. Adam Rulli-Gibbs

    Comparing this against my own solution for p5 raised an interesting (to me anyway!) issue.

    I’m working through Project Euler for the same reason as you – a source of external problems against which to learn Clojure.

    My reasoning for p5 was the same as yours up till and incl. “I realized that this is just another way to state…”. I then went on to decide that I was going to get maximum utility out of the problem by implementing LCM myself.

    Hadn’t really thought about this until I saw your solution, but there’s always the issue, usually decided at gut level, of “at what level do I implement a solution to this for maximum gain”. The answer is obvoiusly going to be personal but probably based upon what you intend to do with the language later, previous programming paradigm practice, current level of knowledge.

    If you know that a problem is similar to the domain in which you’re interested, then it might make sense to start getting used to libraries in the area. Or you might be interested in maximum “learnage” about the core language and libraries (that’s me at the moment). Or if a particular problem seems to easy for you, you might try deliberately ignoring higher level functions in order to practice building up higher abstractions from lower ones. Etc

    (If you want to have a look at mine so far, they’re at https://github.com/status203/euler-clj . Just bear in mind that I’m new to the language (&FP) and have issues with idiomacy 🙂 . I’m currently in revision/tidying/learning dev environment mode so you’ll probably catch up and pass me)

Comments are closed.