如果你能帮助我解决这个问题的任何部分,我将不胜感激。谢谢。
2^0 = 1
2^N = 2^(N-1) + 2^(N-1)
将此定义转换为完全等效的树递归函数,称为二次幂。描述它的渐近时间复杂度并解释为什么它有这个时间复杂度。
现在编写一个名为 tttpo_rec 的函数,它计算完全相同的东西,但它使用线性递归过程,该过程具有 O(n) 时间复杂度,并且还使用 O(n) 空间来进行挂起的操作。
现在编写一个名为 tttpo_iter 的函数,它计算完全相同的东西,但它使用具有 O(n) 时间复杂度并且还使用常数空间的线性迭代过程。
现在假设您想概括前面的定义之一,以便它可以处理任意整数幂,以便您可以计算 2^N、3^N 等。编写一个名为 to-the-power-of 的函数,它接受两个参数并将一个提升到另一个的权力。
这是模板:
;; to-the-power-of: integer integer -> integer
;; This function raises m to the power of n, returning the result.
;; m must be > 0 and n >= 0.
(define (to-the-power-of m n)
...)
(check-expect (to-the-power-of 1 0) 1) ; base case
(check-expect (to-the-power-of 2 3) 8) ; recursive case
(check-expect (to-the-power-of 3 2) 9) ; recursive case
我们将再添加一个限制:您不能使用 * 运算符;您只能使用以下递归函数进行乘法运算:
;;; multiply: integer integer -> integer
;; This function multiplies two non-negative integers, returning the result.
;; Its time complexity is O(a).
(define (multiply a b)
...)
编写函数,并描述其时间复杂度和原因。