• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

recursive algorithm for finding m to power of n?

Status
Not open for further replies.

7imz

Member
i'm trying to write a scheme program where given two integers, m and n, i find m to the power on n... but my brain is all mush now :(... i can't think of anything... any help would be greatly appreciated...thanks
 

fart

Savant
i guess i'm doing your homework for you but...

f(m,n) = f(m,n-1)*m

where f(m, 0) = 1, n >= 0

brain science rocketry this is not.
 

fart

Savant
define (pow m n) if (= n 0) 1 (pow m (- n 1))

note i left the n < 0 case out for you to figure out. it's not very hard. good lucky crazy guy.
 

Jesiatha

Member
SteveMeister said:
Safety check.

The problem is that this returns an incorrect value for n < 0. I would either define m as a float/double and add a case "if (n < 0) return power(m, n+1)/m;", or do something to assert that the n passed in is non-negative.
 

teh_pwn

"Saturated fat causes heart disease as much as Brawndo is what plants crave."
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...
 

Jesiatha

Member
teh_pwn said:
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...

If the compiler implements tail recursion, the compiled code will be exactly the same (loop vs. recursion).
 

rastex

Banned
teh_pwn said:
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...


And this is obviously for teaching reasons I'm sure they just started recursion. Everything recursive can be flattened to a loop but recursive algorithms are far more elegant most of the time, especially with tree traversals.
 

maharg

idspispopd
teh_pwn said:
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...

Scheme is a functional language. Recursion is, if anything, the normal way of doing most operations in most functional languages.

A way of doing it that would be ok for positive or negative would be to, rather than use -1, use -(n/abs(n)). But that would almost certainly be slower unless n is a constant and the function is generated properly to indicate that. Not sure how well scheme deals with such things.

Recursion is not inherently bad, it just requires a lot more work on the part of the compiler in order to optimize it. C compilers tend to be very simplistic, and the language itself is not well designed for recursion. So the compilers don't tend to go that far in optimization.

Languages like scheme are designed for recursion, and the compilers know it's an area they *have* to be able to optimize well.
 
teh_pwn said:
Don't know why your teacher is asking you to do that with recursion...terribly inefficient. A for loop would do, and there might even be a way to do it with bit shifting...

Didn't you ever do Fibonacci's Sequence in recursion to show how ineffiecient it can be in certain situations? Its the first efficency lesson that I really remember.
 
Status
Not open for further replies.
Top Bottom