Comment detail

コラッツ・角谷の問題 (Nested Flatten)
Arcです。
Core2 Duo E6600/Linux 2^20で6分位です。
定義をメモ化できるdefmemoってのがあるので、それを使ってみたんですが、
自分はあまり上手く使いこなせていないようで、スピードがでません…。
実行結果
(time (collatz-max (expt 2 20)))
;=>
;time: 334690 msec.
;"f(837799) = 524"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
(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))

Index

Feed

Other

Link

Pathtraq

loading...