Skip to content

Instantly share code, notes, and snippets.

@zinark
Created January 14, 2013 21:08
Show Gist options
  • Select an option

  • Save zinark/4533504 to your computer and use it in GitHub Desktop.

Select an option

Save zinark/4533504 to your computer and use it in GitHub Desktop.
List<string> _trace = new List<string>();
int SumNumbers(int from, int to)
{
_trace.Add(string.Format("SumNumbers ({0},{1}) : int {{", from, to));
if (from > to)
{
_trace.Add(string.Format("({0} > {1}) return 0 }}", from, to));
return 0;
}
_trace.Add(string.Format("int total = SumNumbers ({0}, {1})", from + 1, to));
int total = SumNumbers(from + 1, to); // recursive with next
_trace.Add(string.Format("return from + total = {0} + {1} = {2} }}", from, total, from + total));
return from + total; // calculation
}
// Execution Output
SumNumbers (1,3) : int
{
int total = SumNumbers (2, 3)
SumNumbers (2,3) : int
{
int total = SumNumbers (3, 3)
SumNumbers (3,3) : int
{
int total = SumNumbers (4, 3)
SumNumbers (4,3) : int
{
(4 > 3) return 0
}
return from + total = 3 + 0 = 3
}
return from + total = 2 + 3 = 5
}
return from + total = 1 + 5 = 6
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment