| pi exp sqrt |
pi := 3.141592653589793238462643s24.
sqrt := [:x |
| pairs digits mem rem |
pairs := OrderedCollection new.
[x > 0] whileTrue: [pairs addFirst: x \\ 100. x := x // 100].
pairs addAll: (Array new: 24 withAll: 0).
digits := rem := mem := 0.
pairs do: [:pair |
| next found |
rem := rem * 100 + pair.
(0 to: 9) findLast: [:i | rem - ((next := mem * 10 + (found := i)) * i) >= 0].
rem := rem - (next * found).
mem := next + found.
digits := digits * 10 + found].
(digits / 1e24) asScaledDecimal: 24].
exp := [:x |
| val fac mul i delta |
val := fac := i := 1s5.
mul := x := x asScaledDecimal: 5.
[(delta := mul / fac) > 0.00001s5] whileTrue: [
val := val + delta.
mul := mul * x.
fac := fac * (i := i + 1)].
val].
^(1 to: 200)
collect: [:n | n -> (exp value: (pi * (sqrt value: n)))]
thenSelect: [:assoc | (assoc value - (assoc value roundTo: 1)) abs <= 0.0001s4]
"=> {37 -> 199148647.99996s5.
58 -> 24591257751.99999s5.
67 -> 147197952743.99998s5.
163 -> 262537412640768743.99998s5} "
sumim
#3033()
[
Smalltalk
]
Rating1/1=1.00
組み込みの ScaledDecimal(Java でいうところの BigDecimal )を使って、sqrt と exp を自前で用意。sqrt には、開平法を使用しています。1GHz PowerPC (OS X) で、21 分ほどかかりました(^_^;)。
| pi exp sqrt | pi := 3.141592653589793238462643s24. sqrt := [:x | | pairs digits mem rem | pairs := OrderedCollection new. [x > 0] whileTrue: [pairs addFirst: x \\ 100. x := x // 100]. pairs addAll: (Array new: 24 withAll: 0). digits := rem := mem := 0. pairs do: [:pair | | next found | rem := rem * 100 + pair. (0 to: 9) findLast: [:i | rem - ((next := mem * 10 + (found := i)) * i) >= 0]. rem := rem - (next * found). mem := next + found. digits := digits * 10 + found]. (digits / 1e24) asScaledDecimal: 24]. exp := [:x | | val fac mul i delta | val := fac := i := 1s5. mul := x := x asScaledDecimal: 5. [(delta := mul / fac) > 0.00001s5] whileTrue: [ val := val + delta. mul := mul * x. fac := fac * (i := i + 1)]. val]. ^(1 to: 200) collect: [:n | n -> (exp value: (pi * (sqrt value: n)))] thenSelect: [:assoc | (assoc value - (assoc value roundTo: 1)) abs <= 0.0001s4] "=> {37 -> 199148647.99996s5. 58 -> 24591257751.99999s5. 67 -> 147197952743.99998s5. 163 -> 262537412640768743.99998s5} "Rating1/1=1.00-0+
[ reply ]