Cursed Recursives

Started by
17 comments, last by ToohrVyk 16 years, 11 months ago
Quote:
Or indeed rational powers ;-) But I think the exercise would be looking primarily for the naturals. I agree the method is sound, although I think you're better treating the 0 case as the end case.


I agree 100%, which is why I personally don't believe this is a good exercise to demonstrate the usefulness of recursion, since (ideally) too many conditions must be checked for within the recursive function. But then again, I haven't written any C++ books, so who am I to judge :)
~Argonaut________________________________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
Advertisement
Quote:Original post by argonaut
Something like this?

*** Source Snippet Removed ***

*fixed*

I dunno, too early for my brain to functionalize recursion. Oh snap ... its 13:21 :)


Hmm that wouldn't work , power(2,0) should return 1, not 2 as your would.

perhaps something like ....
int power(int i, unsigned int p) {    if (p>0) return i*power(i,p-1);    else return 1;}
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by argonaut
Quote:
Or indeed rational powers ;-) But I think the exercise would be looking primarily for the naturals. I agree the method is sound, although I think you're better treating the 0 case as the end case.


I agree 100%, which is why I personally don't believe this is a good exercise to demonstrate the usefulness of recursion, since (ideally) too many conditions must be checked for within the recursive function.


Indeed. Another exercise I'd suggest the OP tries if they want would be the factorial function, that's the one that looks like
0! = 1
1! = 1 * 1 = 1
2! = 2 * 1 = 2
3! = 3 * 2 = 6
4! = 4 * 6 = 24
5! = 5 * 24 = 120
...
[TheUnbeliever]
Some say never demonstrate recursivity on problems that can be solved easily with iteration. So, Towers of Hanoi all the way!
To understand recursion you must first understand recursion.
The best way to do a power function is to cut the powers in half like such

x^n = x^(n/2) * x^(n/2);
there are two cases that will happen when n is odd and when n is even
you have to mod it to find out if it's even or odd so if mod(n, 2) = 0
then it is even and you just do
x^(n/2) * x^(n/2);
and if it's odd then you do
x^(n/2) * x^(n/2) * x;

here is an example:

double power(double x, int n)
{
double exp;
if(x == 0)
{
if(n < 0)
return 0;
}
else if (n == 0)
return 1;

else if (n > 0)
{
exp = power(x, n/2);
exp *= exp;
if (n % 2 > 0)
exp *= x;
return exp ;
}
else if (n < 0)
{
exp = power(x, -n/2);
exp *= exp;
if(-n % 2 > 0)
exp *= x;
return 1/exp;
}
}

[Edited by - Darken Mind on May 19, 2007 5:06:50 PM]
p.s. any problem that can be solved with recursion can be solved with
iteration, however sometimes it's much much simpler to use recursion.
The real gains from using recursion is simplicity and or running time
for instance the function that just showed runs in big O(log n) time
where iteration runs in big O(n) time. Which means that one runs in logarithmic time versus linear time.
why not use pow()?
other wise
try this

int raisetopower(int num,int power);{    int n=1;    for(int i=0;i<power;i++)    {        n*=num    }    return n;}
Quote:Original post by guardian24
Some say never demonstrate recursivity on problems that can be solved easily with iteration. So, Towers of Hanoi all the way!


# Discs are numbered 1 through N, smallest to largest.to check_parity(disc, pole, N):  if pole is not empty: return disc % 2 == (disc at top of pole) % 2.  if pole != start pole: return false.  return disc % 2 == (N + 1) % 2.to hanoi(N):  possible moves: (1->2), (1->3), (2->1), (2->3), (3->1), (3->2).  previous move = nil.  Repeat (2**N)-1 times:    candidate move = nil.    disc to move = 0.    For each move in possible moves:      my disc = disc that would be moved by this move.      If move is inverse of previous move: continue.      If my disc > disc at top of target pole: continue.      If check_parity(my disc, target pole, N): continue.      If disc to be moved > my disc:        disc to move = my disc.        candidate move = move.    Assert candidate move != nil.    previous move = candidate move.    Make candidate move.


OK, I guess it's not *easy* [grin]
Quote:Original post by guardian24
Some say never demonstrate recursivity on problems that can be solved easily with iteration. So, Towers of Hanoi all the way!


Some say never demonstrate recursivity in languages that suck at being recursive. [wink]

This topic is closed to new replies.

Advertisement