Comment detail

分数を小数に展開 (Nested Flatten)
naruse さんの Ruby 版のまんまパクリですが(^_^;)。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
| a b result memo index |
a := 3.
b := 14.
result := '0.'.
memo := OrderedCollection new.
[a isZero] whileFalse: [
   memo add: a.
   a := a * 10.
   result := result, (a // b) printString.
   a := a \\ b.
   (index := memo indexOf: a) > 0
      ifTrue: [^(result first: index + 1), '{', (result allButFirst: index + 1), '}']].
^result

"=> '0.2{142857}' "

Index

Feed

Other

Link

Pathtraq

loading...