計算機科学のブログ

Building Abstractions with Functions - Formulating Abstractions with Higher-Order Functions - Functions as General Methods - infinite continued fraction, tangent

Structure and Interpretation of Computer Programs: JavaScript Edition(Harold Abelson(著)、Gerald Jay Sussman(著)、Julie Sussman(著)、The MIT Press)のChapter 1(Building Abstractions with Functions)、1.3(Formulating Abstractions with Higher-Order Functions)、1.3.3(Functions as General Methods)、Exercise 1.39の解答を求めてみる。

コード

function square(x) {
    return x * x;
}
function cont_frac(n, d, k) {
    function iter(i, result) {
        return i < 0 ?
            result :
            iter(i - 1, n(i) / (d(i) + result));
    }
    return iter(k, 0);
}
function tan_cf(x, k) {
    return cont_frac(
        i => i === 0 ? x : - square(x),
        i => 2 * i + 1,
        k
    )
}

function iter(x, m) {
    function f(k) {
        if (k < m) {
            console.log(`k = ${k} ${tan_cf(x, k)}`);
            f(k + 1);
        }
    }
    console.log('*'.repeat(50));
    console.log(`tan ${x}`);
    f(0);
}

iter(0, 20);
iter(1 / 2, 20);
iter(Math.PI / 2, 20);
iter(3 / 2, 20);
iter(Math.PI, 20);

入出力結果(Terminal, Zsh)

% node answer1.39.js
**************************************************
tan 0
k = 0 0
k = 1 0
k = 2 0
k = 3 0
k = 4 0
k = 5 0
k = 6 0
k = 7 0
k = 8 0
k = 9 0
k = 10 0
k = 11 0
k = 12 0
k = 13 0
k = 14 0
k = 15 0
k = 16 0
k = 17 0
k = 18 0
k = 19 0
**************************************************
tan 0.5
k = 0 0.5
k = 1 0.5454545454545455
k = 2 0.5462962962962963
k = 3 0.5463024650233178
k = 4 0.5463024897807507
k = 5 0.5463024898436799
k = 6 0.5463024898437904
k = 7 0.5463024898437905
k = 8 0.5463024898437905
k = 9 0.5463024898437905
k = 10 0.5463024898437905
k = 11 0.5463024898437905
k = 12 0.5463024898437905
k = 13 0.5463024898437905
k = 14 0.5463024898437905
k = 15 0.5463024898437905
k = 16 0.5463024898437905
k = 17 0.5463024898437905
k = 18 0.5463024898437905
k = 19 0.5463024898437905
**************************************************
tan 1.5707963267948966
k = 0 1.5707963267948966
k = 1 8.847913472585706
k = 2 100.6483883471998
k = 3 2293.32609388008
k = 4 86540.2140642226
k = 5 4822492.715156642
k = 6 370721673.05399096
k = 7 37528913650.47633
k = 8 4832129612041.284
k = 9 744656605476677.9
k = 10 Infinity
k = 11 Infinity
k = 12 Infinity
k = 13 Infinity
k = 14 Infinity
k = 15 Infinity
k = 16 Infinity
k = 17 Infinity
k = 18 Infinity
k = 19 Infinity
**************************************************
tan 1.5
k = 0 1.5
k = 1 6
k = 2 12.750000000000009
k = 3 14.042553191489363
k = 4 14.100000000000009
k = 5 14.101396792550448
k = 6 14.101419673192037
k = 7 14.101419944708303
k = 8 14.101419947154293
k = 9 14.101419947171616
k = 10 14.101419947171719
k = 11 14.101419947171719
k = 12 14.101419947171719
k = 13 14.101419947171719
k = 14 14.101419947171719
k = 15 14.101419947171719
k = 16 14.101419947171719
k = 17 14.101419947171719
k = 18 14.101419947171719
k = 19 14.101419947171719
**************************************************
tan 3.141592653589793
k = 0 3.141592653589793
k = 1 -1.3719535231570001
k = 2 -0.3645065199873463
k = 3 -0.08193036267194619
k = 4 -0.011020481131208506
k = 5 -0.0009173474877210166
k = 6 -0.00005250235469676745
k = 7 -0.0000022198002114450048
k = 8 -7.25577315672916e-8
k = 9 -1.893214149359168e-9
k = 10 -4.040435309397373e-11
k = 11 -7.192294319684479e-13
k = 12 -1.102592289574023e-14
k = 13 -2.8271597168564594e-16
k = 14 -1.4135798584282297e-16
k = 15 -1.4135798584282297e-16
k = 16 -1.4135798584282297e-16
k = 17 -1.4135798584282297e-16
k = 18 -1.4135798584282297e-16
k = 19 -1.4135798584282297e-16
%