Skip to content

Instantly share code, notes, and snippets.

@ir-norn
Last active April 28, 2016 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ir-norn/7183f5232e3939a243d2099fc932cb57 to your computer and use it in GitHub Desktop.
Save ir-norn/7183f5232e3939a243d2099fc932cb57 to your computer and use it in GitHub Desktop.
アッカーマン関数 再帰 と ループ
# ループ
def func a , b
stack = []
loop do
if a == 0
b += 1
if stack.empty?
return b
else
a = stack.pop
end
elsif b == 0
a -= 1
b = 1
else
stack.push a - 1
b -= 1
end
end
end
p func 3 , 3
# 再帰
def f a , b
if a == 0
b + 1
elsif b == 0
f a - 1 , 1
else
f a - 1 , f( a , b - 1 )
end
end
p f 3 , 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment