(def collatz-max (num)
((afn (num i n highest)
(if (> i num)
(prn (string "f(" n ") = " highest))
(let cur (collatz i)
(if (< highest cur)
(self num (+ i 1) i cur)
(self num (+ i 1) n highest)))))
num 1 0 0))
(defmemo collatz (n)
((afn (res cnt)
(if (is res 1)
cnt
(self (if (even res)
(/ res 2)
(+ (* res 3) 1))
(+ cnt 1))))
n 0))
mc
#5589()
[
Arc
]
Rating0/0=0.00
Core2 Duo E6600/Linux 2^20で6分位です。
定義をメモ化できるdefmemoってのがあるので、それを使ってみたんですが、
自分はあまり上手く使いこなせていないようで、スピードがでません…。
実行結果
(time (collatz-max (expt 2 20)))
;=>
;time: 334690 msec.
;"f(837799) = 524"
(def collatz-max (num) ((afn (num i n highest) (if (> i num) (prn (string "f(" n ") = " highest)) (let cur (collatz i) (if (< highest cur) (self num (+ i 1) i cur) (self num (+ i 1) n highest))))) num 1 0 0)) (defmemo collatz (n) ((afn (res cnt) (if (is res 1) cnt (self (if (even res) (/ res 2) (+ (* res 3) 1)) (+ cnt 1)))) n 0))Rating0/0=0.00-0+
[ reply ]