如果你能幫助我這個問題的任何部分,我將不勝感激。謝謝。樹遞歸/時間複雜度計劃分配
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-這需要兩個論點,並且提出另一個論點的力量。
這裏是模板:
;; 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)
...)
寫的功能,並介紹它的時間複雜度和原因。