Problem 012: Highly divisible triangle number

From Project Euler:

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
A way to solve this problem is fairly straight-forward: progress through all the triangle numbers, figure out their divisors, and count them. A simple brute force method is to just enumerate all the numbers up to $\sqrt{n}$, and for each one that divides $n$, add 2 to our count (one for this number, and one for $n$ divided by it). The corner case is if $n$ is square, just count the square root once:
(defn num-divisors [n]
  (loop [current 1
         total 0]
    (cond
      (> current (Math/sqrt n)) total
      (== current (Math/sqrt n)) (+ 1 total)
      :else (recur
              (+ 1 current)
              (+ total
                 (if (== 0 (mod n current)) 2 0))))))
We can then do a simple search through all the triangle numbers to get the first one that has at least 500 divisors:
(defn search-brute-force [limit]
  (loop [n 1
         triangle 1]
    (let [d (num-divisors n)]
     (if (> d limit)
       triangle
       (recur (+ 1 n)
              (+ triangle (+ 1 n)))))))
Unfortunately, this goes well past the one minute limit. Let's see if we can speed this up a bit. Take the formula for a triangle number $T_n$:
$$
T_n = \sum_{i=1}^n = \frac{n(n + 1)}{2}
$$
From this formula we can infer a few other interesting details. Whenever you have two sequential numbers one of them will be even and the other will be odd. Any even number will be divisible by 2, so the 2 in the denominator will get cancelled out by that and we'll be left with a divisor of $T_n$. The odd number left over will also be a divisor.

To write this out more explicitly, if $n$ is even then $\frac{n}{2}$ and $n + 1$ are divisors of $T_n$; if $n$ is odd then $n$ and $\frac{n + 1}{2}$ are divisors of $T_n$.

Next we'll want to dig into the properties of divisors. Any number can be broken down into a product of prime numbers, and some of those primes might be duplicated (for example, 4 = 2 * 2). So we can write any number $n$ like this:
$$
n = p_1^{c_1} * p_2^{c_2} * ...
$$
Then for each prime $p_i$, there's going to be $c_i$ factors of $n$ from that prime: $p_i$, $p_i^2$, $p_i^3$, all the way up to $c_i$. Each one of those can multiply with every other factor of $n$ to produce another factor (for example with $n = 28$ you can have $2 * 7$ to produce 14). So the number of divisors $d(n)$ is $c_i + 1$ multiplied by all the other possibilities:
$$
d(n) = (c_1 + 1) * (c_2 + 1) * ...
$$
Lastly, if you have two numbers $a$ and $b$ that are co-prime with one another, this means that they do not share any divisors other than 1. Therefore none of the $p_i$s are the same, so the number of divisors is just the product of the number of divisors for $a$ and $b$ separately. Since $n$ and $n + 1$ are necessarily coprime, we can just multiply the number of divisors of $n$ with the number of divisors of $n + 1$, dividing the even one by 2 to remove the denominator in the formula for $T_n$:
$$
d(T_n) = \left\{
\begin{array}{ll}
d(\frac{n}{2}) * d(n + 1) & n\ even \\
d(n) * d(\frac{n + 1}{2}) & n\ odd
\end{array}
\right.
$$
These two numbers should be quite a lot smaller than $T_n$, so the search time will be much faster.

Let's write out a little function to calculate the number of divisors of a triangle number:
(defn num-triangle-divisors [n]
  (if (even? n)
    (* (num-divisors (quot n 2))
       (num-divisors (+ 1 n)))
    (* (num-divisors n)
       (num-divisors (quot (+ 1 n) 2)))))
And then tweak our search function to use this instead:
(defn search-triangle [limit]
  (loop [n 1
         triangle 1]
    (let [d (num-triangle-divisors n)]
     (if (> d limit)
       triangle
       (recur (+ 1 n)
              (+ triangle (+ 1 n)))))))
This one is much faster, even with the 3s or so it takes for lein to start up:
$ time lein run
76576500

real 0m3.848s
user 0m4.687s
sys 0m0.355s

Problem 011: Largest product in a grid

From Project Euler:
In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
This one doesn't really require any trickery, and can even be done by hand. That goes against our spirit of writing code to solve everything though, so here's a solution. We scan through each row except for the last 3, collecting the products in four directions: horizontally, vertically, and the diagonal directions left and right. Then we just take the max of all these products:
(def Grid
  [[ 8  2 22 97 38 15  0 40  0 75  4  5  7 78 52 12 50 77 91  8]
   [49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48  4 56 62  0]
   [81 49 31 73 55 79 14 29 93 71 40 67 53 88 30  3 49 13 36 65]
   [52 70 95 23  4 60 11 42 69 24 68 56  1 32 56 71 37  2 36 91]
   [22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80]
   [24 47 32 60 99  3 45  2 44 75 33 53 78 36 84 20 35 17 12 50]
   [32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70]
   [67 26 20 68  2 62 12 20 95 63 94 39 63  8 40 91 66 49 94 21]
   [24 55 58  5 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72]
   [21 36 23  9 75  0 76 44 20 45 35 14  0 61 33 97 34 31 33 95]
   [78 17 53 28 22 75 31 67 15 94  3 80  4 62 16 14  9 53 56 92]
   [16 39  5 42 96 35 31 47 55 58 88 24  0 17 54 24 36 29 85 57]
   [86 56  0 48 35 71 89  7  5 44 44 37 44 60 21 58 51 54 17 58]
   [19 80 81 68  5 94 47 69 28 73 92 13 86 52 17 77  4 89 55 40]
   [ 4 52  8 83 97 35 99 16  7 97 57 32 16 26 26 79 33 27 98 66]
   [88 36 68 87 57 62 20 72  3 46 33 67 46 55 12 32 63 93 53 69]
   [ 4 42 16 73 38 25 39 11 24 94 72 18  8 46 29 32 40 62 76 36]
   [20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74  4 36 16]
   [20 73 35 29 78 31 90  1 74 31 49 71 48 86 81 16 23 57  5 54]
   [ 1 70 54 71 83 51 54 69 16 92 33 48 61 43 52  1 89 19 67 48]])

(def N (count Grid))

(defn search []
  (apply max
    (for [x (range N)
          y (range (- N 4))
          :let
          [horiz (if (< x (- N 4))
                    (->> (range 4)
                         (map #(nth (nth Grid y) (+ x %)))
                         (reduce *))
                    0)
           vert (if (< y (- N 4))
                   (->> (range 4)
                        (map #(nth (nth Grid (+ y %)) x))
                        (reduce *))
                   0)
           ldiag (if (and (>= x 3)
                           (< y (- N 4)))
                    (->> (range 4)
                         (map #(nth (nth Grid (+ y %)) (- x %)))
                         (reduce *))
                   0)
           rdiag (if (and (< x (- N 4))
                           (< y (- N 4)))
                    (->> (range 4)
                         (map #(nth (nth Grid (+ y %)) (+ x %)))
                         (reduce *))
                   0)]]
      (max horiz vert ldiag rdiag))))
This runs and gives the correct result (takes about 5s for lein/JVM to get ready):
$ time lein run
70600674

real 0m5.800s
user 0m7.340s
sys 0m0.404s

Problem 010: Summation of primes

From Project Euler:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
My initial approach to this was to do something using the Sieve of Eratosthenes, and my solution was something like this:
(defn prime-sieve-sum [n]
  (loop [nums (vec (concat [2] (range 3 n 2)))
         total 0]
    (if (empty? nums)
      total
      (let [current (first nums)]
        (recur (vec (remove #(== 0 (mod % current)) (rest nums)))
               (+ current total))))))
This keeps a vector of all the possible primes, and filters out any multiples as it goes along. The downside is that this was very slow: it took about 10 minutes to run on my machine with $n$ at 2 million.

My guess is that since it is constantly creating new vectors, there is a lot of memory copying going on. A better approach might be to keep a large vector of bools and set them to false as each multiple is encountered. This doesn't jive well with Clojure's immutable vectors, but you can fall back to Java's ArrayList if you want to do this approach.

Instead I just decided to brute force it. Using the is-prime? function from Problem 003, I just scanned across:

(defn prime-sum [n]
  (loop [current 3
         total 2]
    (if (>= current n)
      total
      (recur (+ 2 current)
             (+ total (if (is-prime? current)
                        current
                        0))))))
This gets the job done much more quickly (about 5s overhead for lein/JVM):
$ time lein run
Running...
142913828922

real 0m21.508s
user 0m22.936s
sys 0m0.530s

Problem 009: Special Pythagorean Triplet

From Project Euler:
A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which, $$ a^2 + b^2 = c^2 $$ For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. Find the product $abc$.


For this one, we could go ahead and dive into properties of Pythagorean triplets, of which there are many. You can read the Wikipedia page about them and get many hints on how to do this problem more efficiently than what I'm going to show you, and in fact in later articles we'll want to take advantage of these.

However as we say in software engineering: if the sub-optimal solution is good enough and more clear than more efficient solutions, then consider just using the sub-optimal solution. In this case, we can just use Clojure's for comprehension to give us an answer quickly:
(defn find-triplet [s]
  (first
    (for [a (range 1 s)
          b (range (+ a 1) (- s a))
          :let [c (- s a b)]
          :when (== (* c c)
                    (+ (* a a) (* b b)))]
      (list a b c))))

(println (reduce * (find-triplet 1000)))
This runs fast enough (remember that the JVM takes about 1.5s to start up on my machine):
$ time clj 9.clj
31875000

real 0m1.635s
user 0m3.341s
sys 0m0.084s

Problem 008: Largest product in a series

From Project Euler:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

This one was pretty straightforward. You could technically do it using string manipulation, but it's easy and fast to do it with numeric manipulation. First step was just to copy-paste the giant number into a file, and load it into a constant:
(def N
  (read-string (clojure.string/replace (slurp "big_number.txt") "\n" "")))
I also wrote a few helper functions. Some of these are available through libraries, but setting up a Leiningen project for everything is a bit tedious for functions that are small:
(defn pow [a b] (reduce * 1 (repeat b a)))

(defn prod [n]
  "Multiply the digits of `n` together."
  (if (< n 10)
    n
    (* (mod n 10) (prod (quot n 10)))))
Once you do that, you can just loop across $N$, chopping off the last $k$ digits and seeing if the product of those digits is bigger than anything you've seen before:
(defn top [_n num-digits]
  (let [mask (pow 10 num-digits)]
    (loop [n _n best 0]
      (if (== n 0)
        best
        (recur (quot n 10)
               (max (prod (mod n mask))
                    best))))))

(println (top N 13))
This runs pretty quick (remember it takes ~1.5s for the JVM to start up):
$ time clj 8.clj 
23514624000N

real 0m1.569s
user 0m3.196s
sys 0m0.111s

Problem 007: 10001st prime

From Project Euler:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

My initial approach to this was to use a Sieve of Eratosthenes to efficiently find all the prime numbers up to a given limit, however that ended up spending a lot of processing in manipulating lists. Turns out there are faster ways to do it.

I also discovered that Clojure does not have tail-call recursion, you need to use the loop and recur constructs to avoid getting stack overflows.

There are two optimizations here. The first is that we'll skip the first prime 2, since it's obviously not the 10001st prime and it is an anomaly that doesn't let us use the next optimization, which is that all primes greater than 3 have the form $6k \pm 1$. Instead of iterating through each number or even each odd number, we'll iterate through all numbers of that form.

Here's the code:
(defn nth-prime [n]
  (loop [i (- n 2)
         k 1
         s -1]
    (let [p (+ (* 6 k) s)
          next-s (if (= s 1) -1 1)
          next-k (if (= s 1) (+ k 1) k)]
      (cond
        (not (is-prime? p)) (recur i next-k next-s)
        (= i 0) p
        :else (recur (- i 1) next-k next-s)))))
Running this code is actually surprisingly slow:
rob@alien ~/code/euler $ time clj 7.clj 
104743

real 0m16.076s
user 0m17.930s
sys 0m0.349s
I'll have to do some profiling to figure out why this is the case. When translating the code as-is to Python it takes 0.176s, and Python isn't exactly known for speed.

Problem 006: Sum Square Difference

From Project Euler:
The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

The brute-force algorithm for this is pretty simple, just run a loop and calculate the result:
(def N 101)

(defn square [x] (* x x))

(println (- (square (reduce + (range 1 N)))
            (->> (range 1 N)
                 (map square)
                 (reduce +))))
This runs pretty fast (note that the JVM takes 1.5s to start up on my machine):
$ time clj 6.clj
25164150

real 0m1.517s
user 0m3.001s
sys 0m0.133s
We can do better though. As we saw in Problem 001, we can use a formula to calculate the sum of numbers from 1 to $N$:
$$
\sum^n_{i=1} i = \frac{n(n+1)}{2}
$$
Or in Clojure:
(defn sumto [n] (quot (* n (+ n 1)) 2))
If we square this value, we get the square of the sums (the first value in our subtraction). This is O(1) rather than O(n).

Turns out there is also a formula for the sum of squares of a sequence:
$$
\sum^n_{i=1} i^2 = \frac{n(n+1)(2n+1)}{6}
$$
For more details, see here.

Writing this out in Clojure:
(defn sumto-sq [n] (quot (* n
                            (+ n 1)
                            (+ (* 2 n) 1))
                         6))
Combining them all together:
(def N 100)
(println (- (square (sumto N))
            (sumto-sq N)))
This also runs fast, and should run fast for any number that fits into a 64-bit integer1:
$ time clj 6.clj
25164150

real 0m1.583s
user 0m3.043s
sys 0m0.140s



1 We may have to algebraically manipulate the formula a bit to avoid overflows but it should be doable.

Problem 005: Smallest Multiple

From Project Euler:
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?
This is a pretty straight-forward problem: we're just trying to find the least-common multiple (LCM) of a set of numbers. To find the LCM, it is just the product of the two numbers $a$ and $b$ divided by their greatest common divisor (GCD). If we think for a second about why this works: any number is just a product of prime factors. A prime number is just a product of itself and 1, composite numbers are a more complex sequence: 12 is a product of 2 * 2 * 3, 8 is a product of 2 * 2 * 2. When you multiply the two numbers together, you're a making a long sequence of prime factors that will result in the product of $a$ and $b$.

This product however is not the least common multiple, because there could be factors in common between $a$ and $b$: both 8 and 12 have a common factor of 2 * 2, if we simply multiplied them together we'd get 96 when the LCM is actually 24. We should divide by the GCD (2 * 2) to filter out these common factors. This gives us the correct result: 96 / 4 = 24.

Here's the code, which uses the Euclidean algorithm to find the GCD of two numbers:
(defn gcd [a b]
  (cond 
    (< a b) (gcd b a)
    (== 0 b) a
    :else (gcd b (mod a b))))

(defn lcm [a b]
  (if (or (== a 0) (== b 0))
    0
    (quot (* a b) (gcd a b))))
Then we can just do a reduce to get the LCM of the range:
(println (reduce lcm (range 1 21)))
This runs pretty fast (note that on my machine the JVM takes about 1.5s to start up):
$ time clj 5.clj
232792560

real 0m1.585s
user 0m3.136s
sys 0m0.151s
An alternative approach is to use something like the Sieve of Eratosthenes (SoT). This is an algorithm for finding prime numbers, but we can use the same idea here. The way the SoT works is you take a list of numbers from 2 to $N$. For each number $i$, you loop through the rest of the list, removing any multiple of $i$. After the first pass we'll have removed all numbers divisible by 2, after the second pass all numbers divisible by 3, etc. What we'll be left with is just a list of prime numbers. We'll apply the same idea here, but instead of removing the number if it is divisible, we'll divide out our current factor.
(defn lcm-sieve [xs]
  (if (empty? xs) ()
    (let [head (first xs)
          tail (rest xs)]
      (cons head
            (lcm-sieve (map #(if (== 0 (mod % head))
                               (quot % head)
                               %)
                              tail))))))
This algorithm efficiently gives us the list of prime factors for the LCM:
user=> (lcm-sieve (range 1 11))
(2 3 2 5 1 7 2 3 1)
These can be combined using reduce:
(println (->> (range 1 21)
              (lcm-sieve)
              (reduce *)))
This gives the correct result:
rob@alien ~/code/euler $ time clj 5.clj
232792560

real 0m1.549s
user 0m3.014s
sys 0m0.143s
Both methods seem quite fast, I hit integer overflow on my machine without either of them taking more than half a second. The reason I dove into the second one is that the SoT algorithm is the basis for a lot of my solutions in the future, so it's good to see a version of it now.

Problem 004: Largest Palindrome Product

From Project Euler:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.
For this one we'll need to define a few helpers. First, we need a function that determines if a number is palindromic. In order to do that, we'll need to create a function to reverse a number. We could do this easily by converting the number to a string, reversing it, and then converting it back to a number (to avoid issues with leading zeroes) but that's cheating and we can just do it with integer math. Each iteration we have an accumulated number, we just take modulo 10 of our current number and add it to 10 times our accumulator, and then divide our number by 10 to chop off that digit:
(defn reverse-num [n]
  (letfn [(helper [i reversed]
            (if (== i 0)
              reversed
              (helper (quot i 10)
                      (+ (mod i 10)
                         (* 10 reversed)))))]
    (helper n 0)))
We can then use that to define is-palindromic?:
(defn is-palindromic? [n] (== n (reverse-num n)))
Once we have those, we can (mostly) brute force things to get the correct result. We can use a for comprehension to get all the numbers that are products of two 3-digit numbers. We'll do one optimization which is where the second variable y only ranges from the first variable x to 999, since multiplication is commutative and once we've checked 101 * 500 we don't need to check 500 * 101.
(def N 1000)

(println (->> (for [x (range 100 N)
                    y (range x N)]
                (* x y))
              (filter is-palindromic?)
              (apply max)))
There are several other optimizations we can do, if you're interested you can look in the forums for all sorts of creative solutions. However, this code gives the result in a reasonable amount of time (note that on my machine the JVM takes about 1.5s to start up):
$ time clj 4.clj 
906609

real 0m1.747s
user 0m3.478s
sys 0m0.176s

Problem 003: Largest Prime Factor

From Project Euler:
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
This is the first problem with primes. There are some good ways to find primes, however for this simple problem we'll use a simple solution. First, let's define a function that tells us if a number $n$ is prime. The way it works is it will test every number $i$ from 2 to $\sqrt{n}$ to see if $n$ is divisible by $i$. If it is, then $n$ is not prime. We only need to go up to $\sqrt{n}$ because all non-prime numbers will have at least one factor between 2 and their square root.
(defn is-prime? [n]
  (->> (range 2 (int (Math/ceil (Math/sqrt n))))
       (some #(== (mod n %) 0))
       (not)))
From here, we can now filter through and find divisors of 600851475143, and figure out which is the maximum.
(def N 600851475143)

(println
  (->> (range 2 (int (Math/ceil (Math/sqrt N))))
       (filter #(== (mod N %) 0))
       (filter is-prime?)
       (apply max)
       ))
This gives us the correct result (note that on my machine the JVM takes about 1.5s to start up):
$ time clj 3.clj
6857

real 0m1.651s
user 0m3.232s
sys 0m0.138s
There are more efficient ways to find primes: one good one to use is a prime sieve. We don't really need that for this problem since the number is fairly small, but for more advanced problems later on we'll want to use that approach.

Problem 002: Even Fibonacci Numbers

From Project Euler:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
There are typically two ways to work with Fibonacci numbers. You can either use the formula to generate the nth Fibonacci number, a formula that involves taking powers of the golden ratio $\phi$; or you can just simply start from the beginning and iterate through them.
For this one, we'll iterate through and accumulate values as we go along. A simple approach here would be to generate the list of Fibonacci numbers that are less than 4 million, filter out the odd ones, then reduce the list. Something like this:
(<<- (gen-fibs-to 4000000)
     (filter even?)
     (reduce +))
This has a time complexity of O(n), but it also has a space complexity of O(n). We can do better than that. Instead of first generating all the Fibonacci numbers, we can instead just stream through, accumulating values as we need to. A common pattern to do this is to define a local function that accepts an accumulator value, and add the even numbers to it. We quit when we've passed our limit.
(defn sum-even-fibs [n]
  (letfn [(accum [a b sum n]
            (if (>= b n)
              sum  ; we've passed the limit, return the total
              (accum b (+ a b)
                     (+ sum (if (even? b) b 0))
                     n)))]
    (accum 1 2 0 n)))

(println (sum-even-fibs 4000000))
Running this program gives us the correct answer very quickly (note that on my machine the JVM takes about 1.5s to start up):
$ time clj 2.clj 
4613732

real 0m1.532s
user 0m3.136s
sys 0m0.096s