Skip to content

Instantly share code, notes, and snippets.

@kennethgeerts
Created February 17, 2015 11:23
Show Gist options
  • Save kennethgeerts/57271b2aa80be28c73d0 to your computer and use it in GitHub Desktop.
Save kennethgeerts/57271b2aa80be28c73d0 to your computer and use it in GitHub Desktop.
recursion
require 'benchmark'
class Integer
def performant_factorial
(1..self).reduce(:*) || 1
end
def recursive_factorial
self == 0 ? 1 : self * (self - 1).recursive_factorial
end
end
N = 8_000
puts 'Performant:'
puts Benchmark.measure {
N.performant_factorial
}
puts 'Recursive:'
puts Benchmark.measure {
N.recursive_factorial
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment