recursion problem

Pages: 12
Change

1
2
3
4
5
for(int i = n - 1; i > n - 1; i--)
{
     recurse_perm(n+1)+recurse_perm(n+1)+1;
     sum+=1;
}

to

1
2
for(int i = 1; i < n; i++)
     sum += recurse_perm(n - i);
Last edited on


[/code]
Last edited on
1
2
3
4

}


Last edited on
Close. You have that unnecessary else again. Delete line 5.

Your max is there to prevent later recursive calls from calling numbers bigger than the previous ones. You should go to less than n and less than or equal to the max.

The sum shouldn't change much. It will still sum the function in the same way the first one did, but now it will sum it with a new max. If we have 2+1, then we want to ignore 1+2. 2 is greater than 1.

You will be missing some weird cases. We went to n-1 before because n-n = 0, which is beyond our base case, and that's why we added 1 to the sum. Now we need to add 1 to the sum again, but we can't do it every time. We only want to do it when we don't hit our max or n.

return should be outside the loop.
are you sure you meant line 5? I edited the problem a while ago
Sorry, it still looked like the original. Line five is still important.

What you had before was really close. This isn't going to work.
it actually works
Well what do you know? It looked like it would go into an infinite loop at lines 7-8, but I guess line 7 doesn't pass or something. I guess it makes sense. How did you figure that out?
when I tested it, I set n and max to the same integer so it wouldn't go through an infinite loop. As long as max is great than n it will work.

Last edited on
I get that. 7 and 8 should probably just be deleted anyways. For some reason it doesn't work without lines 7 and 8, yet they don't appear to actually do anything. I'm curious how you were able to figure everything out. The rest actually makes sense for recursion (unlike the other problem).
Last edited on
Topic archived. No new replies allowed.
Pages: 12