Ramanujan's constant: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added a hyphen to a word.)
Line 84: Line 84:
NB. approximation as a rational number
NB. approximation as a rational number


]RC=: +`%/ 1j1 (#!.1) 262537412640768743 1 1333462407511 1 8 1 1 5 1 4 1 7 1 1 1 9 1 1 2 12 4 1 15 4 299 3 5 1 4 5 5 1 28 3 1 9 4 1 6 1 1 1 1 1 1 51 11 5 3 2 1 1 1 1 2 1 5 1 9 1x
]RC=: +`%/ }: 1j1 (#!.1) 262537412640768743 1 1333462407511 1 8 1 1 5 1 4 1 7 1 1 1 9 1 1 2 12 4 1 15 4 299 3 5 1 4 5 5 1 28 3 1 9 4 1 6 1 1 1 1 1 1 51 11 5 3 2 1 1 1 1 2 1 5 1 9 1x
45120712325606158012363304056579024470785114628332049030433r171863933112440071790625667019825998411698
45120712325606158012363304056579024470785114628332049030433r171863933112440071790625667019825998411698


Line 105: Line 105:
)
)



NB. returns u to at least y significant digits
Digits=: adverb define NB. u Digits y u y is less accurate than u y+1
Digits=: adverb define NB. u Digits y u y is less accurate than u y+1
NB. returns u to at least y significant digits
format=. ' _.' -.~ ((j.~ 50&+) y)&":
format=. ' _.' -.~ ((j.~ 50&+) y)&":
i =. 5
i =. 5
Line 119: Line 118:
)
)


cf=: 0.1&$: :(4 :0) NB. tolerance cf value -> continued fraction approximation of value to tolerance
rationalize_decimal=: 3 :0 NB. rationalize_decimal LITERAL
y=. y -. ' '
Y =. y
i=. y (, i. ]) '.'
X =. 0 >. x
I=. 'x' ,~ i {. y
terms =. 0 $ 0x
whilst. X < | approximation - y do.
F=. 'x' ,~ y }.~ >: i
'term Y' =. <.`([:%1&|)`:0 Y
I +&". (, ' % ' , 'x' ,~ '1' #!.'0'~ 1 j. <:@:#) F
terms =. terms , term
approximation =. +`%/ }: 1j1 #!.1 terms
end.
)
)

assert (-: 0&cf) 649r200
assert 13r4 (-: cf) 649r200



NB. pi is sufficiently fast for partial series recomputation.
NB. pi is sufficiently fast for partial series recomputation.
Line 139: Line 145:


exp=: (1x"_)`((($:~<:)+^%!@x:@])~)@.(0<[) NB. recursive Taylor series x exp y recursively sums x terms of Taylor series for Exp[y], memoization candidate.
exp=: (1x"_)`((($:~<:)+^%!@x:@])~)@.(0<[) NB. recursive Taylor series x exp y recursively sums x terms of Taylor series for Exp[y], memoization candidate.

</pre>
</pre>

<lang>
<lang>
NB. takes the constant beyond the repeat 9s.
NB. takes the constant beyond the repeat 9s.
S=: cf_sqrt&163 Digits 34
S=: cf_sqrt&163 Digits 34
P=: pi Digits 34
P=: pi Digits 34
Y=: rationalize_decimal 37j34":1r8*P*S
Y=: 1e_36 cf 1r8*P*S
f=: exp&Y M. NB. memoize
f=: exp&Y M. NB. memoize
59j40 ": 8 ^~ f Digits 34
59j40 ": 8 ^~ f Digits 34
262537412640768743.9999999999992500584460275237074973516438
262537412640768743.9999999999992500711164316586918409066184
NB. ^
NB. ^
</lang>
</lang>