Logarithmic iteration method using scheme:
#Lang racket;; N is even: b^n = (b^ (n/2))^2(define (square x) (*x x)); Defines the product function (define (fast-EXPT b N); filter (EXPT-iter B N1)) (Define (EXPT-ITER B N A) (Cond (= N0) a); when n=0with a value of 1 (even N) (Expt-iter (square B) (/n2a)); Determine if an even number (odd n) (expt-iter B (-n1)(*( b A)))); Determine if it is an odd number (define (even N) (= (remainder n2)0)) (Fast-expt2 the);; N is odd: b^n = b*b^ (n1)
Recursive and iterative methods for Java implementations:
Public classTest { Public Static voidMain (String args[]) {intEx,ey; Ex= EXPT (122,4); EY= Expt_iter (122, 4, 1); System.out.println (EY); System.out.println (ex); } //Recursive Static intEXPT (intBintN) { intsum; if(n = = 0) return1; Else{sum= (EXPT (b, (n-1))) *(b); returnsum; } } //Iteration Static intExpt_iter (intBintCounterintproduct) { intsum; if(Counter = = 0){ returnproduct; } Else{sum= Expt_iter (b, (counter-1), (b*product)); returnsum; } }}
Construction and interpretation of the n-th-square SICP computer program for logarithmic calculation of B in iterative method 1.16