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 6 can be found here.
Problem 7 of Project Euler is described as:
What is the 10 001st prime number?
(defn is-prime? [n] (cond (<= n 1) false (= n 2) true :else (loop [f 2] (cond (zero? (rem n f)) false (> f (Math/sqrt n)) true :else (recur (inc f)))))) (defn problem7 ( (problem7 10001)) ([n] (first (skip-n (dec n) (filter is-prime? (iterate inc 1))))))
The is-prime? function check to see if the number is prime by checking if any of the numbers from 2 to the square root of the number is a divisor of the number.
The problem7 function is finds the n-th number in the sequence by skiping n-1 items, and then taking the first item of that sequence. This was before a previous post in which someone pointed out that the drop function was available instead of my home rolled skip-n function, so that can be replaced on the update.
Again, I would love comments and suggestions on my solution to this problem, and if there are tweaks to make it more Clojure-ish.
My solution to Problem 8 has been posted.