递归最大公共devisor和递归pascal三角形之间的区别

时间:2018-05-27 11:59:23

标签: scala recursion greatest-common-divisor

有人可以解释一下这两个递归函数是如何彼此不同的吗? 我理解每一个的数学方法,但我不明白为什么gcd()继续工作,直到它找到gcd但pascal()在一轮后停止。 感谢。

最伟大的普通设计师:

def gcd(x: Int, y: Int): Int =
    if (y == 0) x else gcd(y, x % y)

Pascal Triangle

def pascal(c: Int, r: Int): Int = {
      if(c < 0 || r < 0 ||  c > r) 0
      else if(c == 0 || r == 0) 1
      else pascal(c-1, r-1) + pascal(c, r-1)

  }     

1 个答案:

答案 0 :(得分:0)

这是您的pascal - 方法,增加了一些缩进输出,以可视化计算的顺序:

var currentIndentation = 0
def indenting[X](body: => X): X = {
  currentIndentation += 2
  val res = body
  currentIndentation -= 2
  res
}
def indentPrintln(s: String) = println(" " * currentIndentation + s)

def pascal(c: Int, r: Int): Int = {
  indentPrintln("pascal(%d, %d)".format(c, r))
  if(c < 0 || r < 0 ||  c > r) {
    indentPrintln("base case: return 0")
    0
  } else if(c == 0 || r == 0) {
    indentPrintln("base case: return 1")
    1
  } else indenting {
    val a = pascal(c-1, r-1)
    val b = pascal(c, r-1)
    val sum = a + b
    indentPrintln(
      "return sum pascal(%d, %d) + pascal(%d, %d) = %d + %d = %d"
      .format(c-1, r-1, c, r-1, a, b, sum)
    )
    sum
  }
}

println(pascal(3, 5))

对于(3, 5),它会产生以下输出:

pascal(3, 5)
  pascal(2, 4)
    pascal(1, 3)
      pascal(0, 2)
      base case: return 1
      pascal(1, 2)
        pascal(0, 1)
        base case: return 1
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        return sum pascal(0, 1) + pascal(1, 1) = 1 + 1 = 2
      return sum pascal(0, 2) + pascal(1, 2) = 1 + 2 = 3
    pascal(2, 3)
      pascal(1, 2)
        pascal(0, 1)
        base case: return 1
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        return sum pascal(0, 1) + pascal(1, 1) = 1 + 1 = 2
      pascal(2, 2)
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        pascal(2, 1)
        base case: return 0
        return sum pascal(1, 1) + pascal(2, 1) = 1 + 0 = 1
      return sum pascal(1, 2) + pascal(2, 2) = 2 + 1 = 3
    return sum pascal(1, 3) + pascal(2, 3) = 3 + 3 = 6
  pascal(3, 4)
    pascal(2, 3)
      pascal(1, 2)
        pascal(0, 1)
        base case: return 1
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        return sum pascal(0, 1) + pascal(1, 1) = 1 + 1 = 2
      pascal(2, 2)
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        pascal(2, 1)
        base case: return 0
        return sum pascal(1, 1) + pascal(2, 1) = 1 + 0 = 1
      return sum pascal(1, 2) + pascal(2, 2) = 2 + 1 = 3
    pascal(3, 3)
      pascal(2, 2)
        pascal(1, 1)
          pascal(0, 0)
          base case: return 1
          pascal(1, 0)
          base case: return 0
          return sum pascal(0, 0) + pascal(1, 0) = 1 + 0 = 1
        pascal(2, 1)
        base case: return 0
        return sum pascal(1, 1) + pascal(2, 1) = 1 + 0 = 1
      pascal(3, 2)
      base case: return 0
      return sum pascal(2, 2) + pascal(3, 2) = 1 + 0 = 1
    return sum pascal(2, 3) + pascal(3, 3) = 3 + 1 = 4
  return sum pascal(2, 4) + pascal(3, 4) = 6 + 4 = 10
10

希望有所帮助。