CS/SICP in JS

연습문제 1.16

띵킹 2023. 2. 11. 15:26
function fast_expt(b, n) {
    return fast_expt_iter(b, n, 1);
}

function fast_expt_iter(b, counter, product) {
    return counter === 0
        ? product
        : is_even(counter)
        ? fast_expt_iter(b * b, counter/2, product)
        : fast_expt_iter(b, counter-1, b* product);
}

function sqaure(n) {
    return n * n;
}

function is_even(n) {
    return n % 2 === 0;
}

 

728x90

'CS > SICP in JS' 카테고리의 다른 글

연습문제 1.30  (0) 2023.02.18
연습문제 1.17  (0) 2023.02.11
연습문제 1.12  (0) 2023.02.10
연습문제 1.11  (0) 2023.02.10
연습문제 1.10  (0) 2023.02.10