計算機科学のブログ

Building Abstractions with Functions - Functions and the Processes They Generate - Orders of Growth - sine, trigonometric identity

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.2(Functions and the Processes They Generate)、1.2.3(Orders of Growth)、Exercise 1.14の解答を求めてみる。

aについて。

コード

function cube(x) {
    return x * x * x;
}
function p(x, count) {
    return 3 * x - 4 * cube(x);
}
function sine(angle, count) {
    console.log(`${count}回`);
    return !(Math.abs(angle) > 0.1) ?
        angle :
        p(sine(angle / 3, count + 1));
}
sine(12.15, 0);

入出力結果(Terminal, Zsh)

% node answer1.15.js
0回
1回
2回
3回
4回
5回
%

bについて、

  • a/3^n = 0.1
  • 10 a = 3^n
  • log (10a) = n log 3
  • log_3 (10a) = n

よって計算量はO(log(a))