Comment detail
分数を小数に展開 (Nested Flatten)
2147483645 / 2147483647 でOut of Memoryが出ちゃいました。
スタックの事もあるし、考え直します。
副作用無し版。gaucheです。
検索が遅いので、桁が多いと遅くなります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | (use text.tree)
(use srfi-1)
(use srfi-11)
(use gauche.sequence)
(define (div3 a b)
(tree->string (cons "0."
(let loop ((a (* 10 a)) (b b) (r ()) (l ()))
(cond ((find-index (pa$ = a) l) =>
(lambda (m)
(reverse
(call-with-values (cut split-at r (+ m 1))
(cut append '("}") <> '("{") <>)))))
((< a b) (loop (* a 10) b (cons 0 r) (cons a l)))
(else (let-values (((quo rem) (quotient&remainder a b)))
(if (zero? rem)
(reverse (cons quo r))
(loop (* rem 10) b (cons quo r) (cons a l))))))))))
|





katsu
#182()
[
Scheme
]
Rating0/0=0.00
Rating0/0=0.00-0+
2 replies [ reply ]