Converting Nested For loops to a Recursive Function

Hi
Last edited on
I've already tried writing the function but I think what I have is a load of garbage, so unless it'd be worthwhile I'm not going to post it.


Might be a good idea to post it then. If nothing else but give people a chance to let you know how to make it better.

Also It would make more sense to know what these variables are.
If there was something I would say about the code it would be make your variables descriptive. Variables like b, N & p do not tell you or anyone else about what they represent.

When you write large portions of code you won't know which way is up with variables like these.
Last edited on
Furthermore, look at the inmost "dimension":
1
2
3
4
5
6
7
for (k = j; k <= N-i-j; k++)
{
  if(i+j+k == N)
    {
      // ...
    }
}

It can add at most one "record". More simply:
1
2
3
4
5
k = N - i - j;
if ( j <= k )
  {
    // ...
  }


And then some: look at last outmost iteration. i==N. There is no solution, because i <= j <= k, and thus N < i+j+k (if 0<N).

In other words, your current loops do unnecessary work.
Topic archived. No new replies allowed.