How to organise the tracing of this recursion

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Recursion tracing
Click For Summary

Discussion Overview

The discussion revolves around tracing a recursive function defined in C, specifically the function a(int n, int count). Participants explore how to systematically follow the recursion for various input values, particularly focusing on the behavior of the function as the input increases. The conversation includes attempts to clarify the recursion's structure and its implications for output values.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about tracing a(2, 0) and attempts to outline the recursive calls but struggles to maintain clarity.
  • Another participant suggests that the original function may lead to infinite recursion and proposes an alternative version of the function that starts the loop at i=1 and uses i-1 for recursion.
  • A different participant counters that a(2, 0) does not nest forever and claims to have traced it to yield a result of 4, arguing that the recursion eventually reaches n=0.
  • One participant emphasizes the importance of understanding "pass by value" in function parameters to effectively trace recursion.
  • A later post corrects a misunderstanding about the loop condition and provides a detailed breakdown of the calling sequence for a(3, 0), leading to a formula for a(n, 0) that suggests it computes 2^n.

Areas of Agreement / Disagreement

Participants express differing views on the behavior of the original function, with some believing it leads to infinite recursion while others assert it terminates correctly. There is no consensus on the best way to trace the recursion or the implications of the function's structure.

Contextual Notes

Some participants note potential misunderstandings regarding recursion and function parameters, indicating that familiarity with these concepts is crucial for tracing the function accurately. The discussion also highlights the complexity of the recursive calls and the mathematical relationships involved.

transgalactic
Messages
1,386
Reaction score
0
i tried to trace this recurtion for small numbers
but i don't know how to write down
in an organized way

Code:
int a(int n,int count){
  int i;
  for(i=0;i<n;i++)
    count =a(i,count);
  return count+1;
}

a(0,0) is easy its 1
a(1,0) a little harder it returns 2

but when i get to
a(2,0) i get confused and i cand follow one logical path
i don't how to write it down

i tried like that:
a(n=2,count=0):
for (0,0<2)
count=a(0,0)=1
for (1,1<2)
count=a(1,1)

a(1,1)
for (0,0<1)
count=a(0,1)
a(0,1)=2

here i am getting really confused
??
 
Technology news on Phys.org
As written, it appears that a(2, ...) will just nest forever until you run out of stack space. Perhaps you meant to write it as this (for loop starts with i=1, recursive call uses i-1):

Code:
int a(int n,int count){
  int i;
  for(i=1; i<n; i++)
    count =a(i-1,count);
  return count+1;
}
 
Jeff Reid said:
As written, it appears that a(2, ...) will just nest forever until you run out of stack space. Perhaps you meant to write it as this (for loop starts with i=1, recursive call uses i-1):

Code:
int a(int n,int count){
  int i;
  for(i=1; i<n; i++)
    count =a(i-1,count);
  return count+1;
}

Hi Jeff, I don't think it nests forever with n=2. I just traced a(2,0) and I got the result of 4, it wasn't too difficult.

BTW. Note that with the "for i=0, i<n, i++" loop, each time it recurses it only counts up to n-1 so eventually you get to n=0 and starts un-nesting. I don't see the problem.

Anyway it's a hell ugly way to compute 2^n :bugeye:
 
Last edited:
BTW transgalactic. To understand (or trace) recursion you first must fully understand how "pass by value" function paramaters work. If you're not familiar with what this means, and in particular what it means with regard to "no side effects of the functions actual parameters" then you should read up on this before you go any further.
 
I kept thinking <= instead of < in the for loop. (I should never post when I'm sleepy). As was mentioned in a later thread, a (
n, c) returns 2^n + c.

for the case a(3,0), the calling sequence looks like this:
Code:
a(3,...)
 a(0,...)
 a(1,...)
   a(0,...)
 a(2,...)
   a(0,...)
   a(1,...)
     a(0,...)

a(0, ... ) is called 4 times, a(1, ...) is called 2 times, a(2, ...) 1 time, and a(3, ...) 1 time.

a(n,0) = 1 + \sum_{i=0}^{n-1} 2^i

For the summation part:
s(n-1) = 2^0 + 2^1 + 2^2 ... + 2^(n-1)

multiply s(n-1) by 2:
2 s(n-1) = 2^1 + 2^2 + ... + 2^n

subtract s(n-1) from 2 s(n-1):
2 s(n-1) - s(n-1) = 2^n + 2^(n-1) - 2^(n-1) + ... (2^1 - 2^1) - 2^0
s(n-1) = 2^n - 1

a(n) = 1 + s(n-1) = 1 + 2^n -1 = 2^n
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
Replies
20
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K