用clojure解决 euler problem 1

问题描述:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

解决方案1:

(defn sum-3-or-5-multiple-below
  [x]
  "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
   Find the sum of all the multiples of 3 or 5 below 1000."
  (apply +  (filter #(or (= 0 (rem % 3))
                         (= 0 (rem % 5)))
                    (take (- x 1) (iterate inc 1)))))
解决方案2:

(defn sum-3-or-5-multiple 
  [x]
  (loop [current-num 1, sum 0]
    (if (< current-num x)
      (if(or 
           (= 0 (rem current-num 3))
           (= 0 (rem current-num 5)))
        (recur (inc current-num)(+ sum current-num))
        (recur (inc current-num) sum)) sum)))
性能测试:

方案1:

(time (sum-3-or-5-multiple-below 100000))
 ;"Elapsed time: 157.460515 msecs"

方案2:

(time (sum-3-or-5-multiple 100000))
"Elapsed time: 23.742962 msecs"

分析:

方案1首先用iterate 函数重复加1产生整个序列,然后重序列中筛选所出需要的结果再次组成序列,最后把序列中的结果相加得出结论。所以正如预计的,当数据达到百亿的时候,系统陷入长时间等待中,数据再大,内存可能溢出。用 (range 1 x) 代替

(take (- x 1) (iterate inc 1)) ;性能有所提升,但是还是赶不上方案2的性能。

方案2使用了clojure中的尾递归优化技术。参数current-num初始值是1,sum初始值是0,sum用来保存每次递归的累积结果。每次递归,只与上一次递归的结果相关,因此上上一次递归的数据可以垃圾回收,不会造成内存溢出。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值