/***********************************************************
contfrac.c -- 連分数
***********************************************************/
#N88BASIC

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 "...]"

/* ふつうの分数と実数に直す */
reduce_cf(N, b)

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2010年07月19日 22:25