用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 is evenly divisible by 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"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章