MUTUAL RECURSION

In mathematics and computer science, mutual recursion is a form of recursion where two mathematical or computational objects, such as functions or data types, are defined in terms of each other.

The Problem

a(1); 

function a(foo){
  if (foo > 20) return foo; 
  return b(foo + 2); 
}

function b(foo){
  return c(foo) + 1; 
}

function c(foo){
  return a(foo * 2); 
}

The Solution

a(1)
b(3)
c(3) + 1
a(6) + 1 
b(8) + 1 
c(8) + 1 + 1; 
a(16) + 2 
b(18) + 2
c(18) + 2 + 1  == c(18) + 3
a(36) which is greater than 20 so we hit terminating condition
36 + 3 
returns 39

Copy of deck

By gtelljohann

Copy of deck

  • 489
Loading comments...

More from gtelljohann