Created
May 6, 2009 07:41
-
-
Save jballanc/107422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ack(m, n) | |
if m == 0 | |
n + 1 | |
elsif n == 0 | |
ack(m - 1, 1) | |
else | |
ack(m - 1, ack(m, n - 1)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ack_catch(m, n) | |
if m == 0 | |
throw :bottom, n + 1 | |
elsif n == 0 | |
catch :bottom do | |
ack_catch(m - 1, 1) | |
end | |
else | |
catch :bottom do | |
ack_catch(m - 1, ack(m, n - 1)) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bottom = Class.new(Exception) | |
def ack_raise(m, n) | |
if m == 0 | |
raise Bottom, n + 1 | |
elsif n == 0 | |
begin | |
ack_raise(m - 1, 1) | |
rescue Bottom => value | |
return value.message | |
end | |
else | |
begin | |
ack_raise(m - 1, ack(m, n - 1)) | |
rescue Bottom => value | |
return value.message | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ack' | |
require 'ack_catch' | |
require 'ack_raise' | |
require 'benchmark' | |
puts "Value for 'm'?" | |
m = gets.chomp.to_i | |
puts "Value for 'n'?" | |
n = gets.chomp.to_i | |
Benchmark.bm(3) do |test| | |
test.report("Recursive:") { puts "ack(#{m}, #{n}) => #{ack(m, n)}" } | |
test.report("Catch:") { puts "ack_catch(#{m}, #{n}) => #{ack_catch(m, n)}" } | |
test.report("Raise:") { puts "ack_raise(#{m}, #{n}) => #{ack_raise(m, n)}" } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment