用clojure解决euler problem 5

问题描述:

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 isevenly divisibleby all of the numbers from 1 to 20?

即求1到20的所有数的最小公倍数。


解决方案:

(ns euler-problem-5.core
  (:use clojure.contrib.math))

(defn  smallest-lcm-from-1-to
  [num]
  (loop
      [current-num 1 result 1]
    (if (= current-num num)
    (lcm result num)
    (recur (inc current-num) (lcm current-num result)))))

(smallest-lcm-from-1-to 20)

运行结果:232792560

"Elapsed time: 0.301993 msecs"


你可能感兴趣的:(clojure)