Can every recursion be converted into iteration?

前端 未结 17 2523
遥遥无期
遥遥无期 2020-11-22 01:59

A reddit thread brought up an apparently interesting question:

Tail recursive functions can trivially be converted into iterative functions. Other one

17条回答
  •  Happy的楠姐
    2020-11-22 02:17

    Here is an iterative algorithm:

    def howmany(x,y)
      a = {}
      for n in (0..x+y)
        for m in (0..n)
          a[[m,n-m]] = if m==0 or n-m==0 then 1 else a[[m-1,n-m]] + a[[m,n-m-1]] end
        end
      end
      return a[[x,y]]
    end
    

提交回复
热议问题