简体   繁体   中英

How do I convert loops to recursions?

I'm not quite understanding recursions. I'm working on a class program, and I converted most of my program to recursions. So, my professor doesn't want us to use loops and also doesn't want us to use more than one parameter.

The program runs through the hailstone sequence and the function I'm having trouble computes the length of the hailstone sequence. I had it working my way, but wanted to confirm if it was up to his standards. (Is not to his standards.)

I wanted to know if there was a better way I could do this function. The problem I'm having when I tried was I can't use a static variable as it would never reset the counter and would total the hailstone sequences from 1 to n. I've also tried doing it by leaving the count in my function as was, though when I did hand sims (since I refused to believe it reset more than I wanted) and saw it reset everytime. Also, no answer is needed.

I would rather figure it on my own, so I was asking to see if anyone could give me a "formula" or some guide to follow to convert loops to recursions. As reference I'll paste my hailstoneLength code that finds the length of the hailstone sequence. Though, if you would rather tell me how to do it is it okay if you could explain how it works? I'll most likely do some hand simulations to work it out.

Loop:

int hailstoneLength(int n)
{
    int count = 1;
    int u = n;
    while(u != 1)
    {
        u = hailNext(u);
        count++;
    }
    return count;
}

Recursion:

int hailstoneLength(int n, int count = 1) 
{
    int u = n;
    if(u != 1)
    {
        return hailstoneLength(hailNext(u), count + 1);
    }
    else // if u = 1
    {
        return count;
    }
}

I was thinking I needed to make some function that works the count since it cant be a parameter, or in the function without resetting.

Thanks

First of all, you have confused the picture by introducing u as an extraneous variable. The non-recursive function can be simplified to

int hailstoneLength(int n)
{
    int count = 1;
    while(n != 1)
    {
        n = hailNext(n);
        count++;
    }
    return count;
}

From this it is easy to produce a recursive form

int hailstoneLength(int n)
{
    if (n != 1)
        return hailstoneLength(hailNext(n)) + 1;
    else
        return 1;
}

which can be recast to make it clear recursion stops when n == 1 .

int hailstoneLength(int n)
{
    if (n == 1)
        return 1;
    else
        return hailstoneLength(hailNext(n)) + 1;
}

or even (to make it obvious this can be written in a tail recursive form) ....

int hailstoneLength(int n)
{
    if (n == 1) return 1;
    return hailstoneLength(hailNext(n)) + 1;
}

This would do what you need. You don't need a tail-recursive function to keep track of the amount of times the function is recurses, which is essentially what you're keeping track of here.

int hailstoneLength(int n) 
{
    int u = n;
    if(u != 1)
    {
        return 1 + hailstoneLength(hailNext(u));
    }
    else // if u = 1
    {
        return 1;
    }
}

There's no real general formula for breaking down iterative loops to recursive function calls, but eliminating the loop invariant as we're doing here is the general idea.

You've defined your return type as an int, but hailstoneLength(hailNext(u), count + 1) does not seem to be an int.

Instead, what you could do, is return hailstoneLength(hailNext(u+1)), so that on the next loop iteration it will call hailstoneLength(n+1) (I believe that is the general principle behind what you're trying to do). For what it's worth, I don't think you need int u = n.

I have no idea what hailNext does, so I can't help further, but you definitely want to make sure you're returning an int in recursive functions (which generally means you can't have multiple inputs).

Take a look at this recursive function for doing a factorial, for reference:

int myFactorial( int integer)
{
if( integer == 1)
    return 1;
else
   {
   return (integer * (myFactorial(integer-1)));
   }
}

Suppose integer is 5.

It returns (5 * (myFactorial(integer-1)))

So on the next iteration, integer is 4.

So it will return 4 * (myFactorial(integer-1)))

And so on! At the end it will return 5 * 4 * 3 * 2 * 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM