Sub contfrac(x As Double, n As Long, b As *Long) Dim i As Long
b[0] = Fix(x) For i = 1 To n x = 1 / (x - b[i - 1]) b[i] = Fix(x) Next End Sub
Function gcd(x As Long, y As Long) As Long Dim t As Long
While (y <> 0) t = x Mod y: x = y: y = t Wend gcd = x End Function
Sub reduce_cf(n As Long, b As *Long) Dim i As Long Dim f As Long, g As Long, temp As Long, d As Long
f = b[n]: g = 1 For i = n - 1 To 0 Step -1 temp = b[i] * f + g: g = f: f = temp d = gcd(f, g): f = f / d: g = g / d Next Print f ;"/"; g ;"="; f / g End Sub
Const N = 17
Dim i As Long Dim b[N] As Long
/* e = 2.718...の連分数展開 */ contfrac(2.71828182845904524, N, b) Print "e = ["; For i = 0 To N Print b[i];","; Next Print "...]"