Originally posted by Ryan Waggoner:

I was doing a recursive assignment for my class, and came up with the following code...

I was told that this is indeed recursive, but there is a better way to do it. I wasn't given any hints or anything, and Im not sure what I need to do to make it better...

Any hints?

It turns out that there is indeed another way, and it is quite beautiful when you see how it works.

Just a hint to get you started:

If you want say x^7, what you are doing now is

x * x^6, which in turn becomes x * x * x^5 and so on.

Instead, you may note that 7 = 4 + 2 + 1, so that

(x^2)^2 * (x^2) * x would also get you the same result.

hope this helps,

- Anand