コンテンツへスキップ
🛠️ToolsShed

連分数計算機

小数や分数を連分数表記と収束分数に変換。

このツールについて

連分数は、任意の実数をネストされた除算の数列として表現する数学的な記法です。標準的な小数表記とは異なり、連分数は数値の基礎となる算術構造を明らかにし、段階的により優れた有理数近似を提供します。このツールは、3.14159のような小数でも355/113のような分数でも、入力を連分数形式に変換し、数学者がいかに無理数定数への驚くべき精密な有理近似を歴史的に発見してきたかを理解しやすくします。

この計算機は簡単に使用できます。小数を入力するか、分子/分母の形式で分数を入力し、計算したい項の数を指定します。ツールは連分数表記を[a₀; a₁, a₂, ...]の形式で表示し、収束分数の表を生成します。各収束分数は、分母のサイズに対する最良の有理近似を提供し、この性質は航海システム、天文計算、音楽チューニングで利用されています。

連分数は数論と暗号化に特に価値があり、無理数の効率的な近似は微妙な数学的性質を露呈させることができます。特定の無理数(2の平方根など)に現れる周期的パターンは、代数と幾何学への深い結びつきを明らかにします。純粋数学を探究する場合でも、エンジニアリング近似を検証する場合でも、あるいはなぜ355/113がπをそれほど正確に近似するのかについて知りたい場合でも、このツールは抽象的な連分数理論を視覚的でインタラクティブな計算に変換します。

よくある質問

コード実装

import math

def continued_fraction(x: float, max_terms: int = 20) -> list[int]:
    """Compute continued fraction representation of x"""
    terms = []
    remaining = x
    for _ in range(max_terms):
        a = int(math.floor(remaining))
        terms.append(a)
        frac = remaining - a
        if abs(frac) < 1e-10:
            break
        remaining = 1 / frac
        if not math.isfinite(remaining):
            break
    return terms

def convergents(terms: list[int]) -> list[tuple[int, int]]:
    """Compute convergents (rational approximations) from continued fraction terms"""
    conv = []
    h_prev, h_curr = 1, terms[0]
    k_prev, k_curr = 0, 1
    conv.append((h_curr, k_curr))

    for i in range(1, len(terms)):
        a = terms[i]
        h_next = a * h_curr + h_prev
        k_next = a * k_curr + k_prev
        h_prev, h_curr = h_curr, h_next
        k_prev, k_curr = k_curr, k_next
        conv.append((h_curr, k_curr))
    return conv

# Example: pi
pi_terms = continued_fraction(math.pi, 10)
print(f"pi = [{pi_terms[0]}; {', '.join(map(str, pi_terms[1:]))}]")

for num, den in convergents(pi_terms):
    approx = num / den
    error = abs(approx - math.pi)
    print(f"  {num}/{den} = {approx:.10f}  (error: {error:.2e})")

# Golden ratio
phi = (1 + math.sqrt(5)) / 2
print(f"phi terms: {continued_fraction(phi, 8)}")  # All ones!

# sqrt(2)
sqrt2_terms = continued_fraction(math.sqrt(2), 8)
print(f"sqrt(2) terms: {sqrt2_terms}")  # [1; 2, 2, 2, ...]

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.