What is the difference between these two recursive functions?

user3450695

The problem is to find the nth power of x^n of a number x, where n i s a positive integer. What is the difference between the two pieces of code below. They both produce the same result.

This is the code for the first one:

(define (power x n)
  (define (square n) (* n n))
  (cond ((= n 1) x)
        ((even? n)
         (square (power x (/ n 2))))
        (else 
         (* (power x (- n 1)) x))))

This is the second one:

(define (power x n)
  (if (= n 1)
      x 
      (* x (power (- n 1) x))))
chiastic-security

The difference is in the time it takes for the two algorithms to run.

The second one is the simpler, but also less efficient: it requires O(n) multiplications to calculate x^n.

The first one is called the square-and-multiply algorithm. Essentially, it uses the binary representation of the exponent, and uses the identities

x^(ab) = ((x^a)^b)
x^(a+b) = (x^a)(x^b)

to calculate the result. It needs only O(log n) multiplications to calculate the result.

Wikipedia has some detailed analysis of this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the difference between these two recursive ocaml functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference between these two recursive approaches

From Dev

What is the difference between these two functions in Javascript?

From Dev

What is the difference and issues between these two clojure functions?

From Java

What's the difference between these two functions

From Dev

What's the difference between these two functions?

From Dev

What's the difference between these two functions in JavaScript?

From Dev

What is the difference between these two ruby functions?

From Dev

What exactly is the difference between these two javascript functions?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What is the difference between scope of these two functions

From Dev

What are all the difference between these two Javascript functions?

From Dev

Difference Between Two Recursive Methods

From Dev

What is the difference between these two functions using async/await/TPL?

From Dev

What's the difference between these two ways to declare functions?

From Dev

What is the essential difference between the two functions to find the first negative entry?

From Dev

What is the difference between these two ways of changing a functions prototype in JavaScript?

From Dev

What is the essential difference between the two functions to find the first negative entry?

From Dev

What is the difference between these two C functions in terms of handling memory?

From Dev

Javascript : What is the difference between these two fat arrow functions?

From Java

What is the difference between these arrow functions

From Dev

What is the difference between functions and closures?

From Dev

What is the difference between functions and closures?

From Dev

What is difference between 2 functions

From Dev

What is the difference between recursive and recursively enumerable languages

From Java

What is the difference between Cloud Functions and Firebase Functions?

From Dev

What is difference between these two statements?

Related Related

  1. 1

    What is the difference between these two recursive ocaml functions?

  2. 2

    What is the difference between these two recursive functions?

  3. 3

    What is the difference between these two recursive approaches

  4. 4

    What is the difference between these two functions in Javascript?

  5. 5

    What is the difference and issues between these two clojure functions?

  6. 6

    What's the difference between these two functions

  7. 7

    What's the difference between these two functions?

  8. 8

    What's the difference between these two functions in JavaScript?

  9. 9

    What is the difference between these two ruby functions?

  10. 10

    What exactly is the difference between these two javascript functions?

  11. 11

    What is the difference and issues between these two clojure functions?

  12. 12

    What is the difference between these two ruby functions?

  13. 13

    What is the difference between scope of these two functions

  14. 14

    What are all the difference between these two Javascript functions?

  15. 15

    Difference Between Two Recursive Methods

  16. 16

    What is the difference between these two functions using async/await/TPL?

  17. 17

    What's the difference between these two ways to declare functions?

  18. 18

    What is the essential difference between the two functions to find the first negative entry?

  19. 19

    What is the difference between these two ways of changing a functions prototype in JavaScript?

  20. 20

    What is the essential difference between the two functions to find the first negative entry?

  21. 21

    What is the difference between these two C functions in terms of handling memory?

  22. 22

    Javascript : What is the difference between these two fat arrow functions?

  23. 23

    What is the difference between these arrow functions

  24. 24

    What is the difference between functions and closures?

  25. 25

    What is the difference between functions and closures?

  26. 26

    What is difference between 2 functions

  27. 27

    What is the difference between recursive and recursively enumerable languages

  28. 28

    What is the difference between Cloud Functions and Firebase Functions?

  29. 29

    What is difference between these two statements?

HotTag

Archive