Comments on: Recursion and Memoization in Ruby http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/ Ruby Programming Tutorials Thu, 20 Aug 2015 11:42:51 +0000 hourly 1 http://wordpress.org/?v=4.1.7 By: Guess What? (@kofno) http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/#comment-403 Thu, 20 Aug 2015 11:42:51 +0000 http://www.blackbytes.info/?p=2156#comment-403 You can actually turn TCO on in ruby, if you are so inclined. That would make it possible to write the recursive example w/out eating up stack: http://nithinbekal.com/posts/ruby-tco/

]]>
By: Jesus Castello http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/#comment-402 Thu, 20 Aug 2015 03:30:54 +0000 http://www.blackbytes.info/?p=2156#comment-402 Thanks for pointing that out, I was aware of the issue but wasn’t sure how to put it into words. I ran some tests and it seems like the stack size is always the same on every machine. On a Linux VM the ulimit parameter for max stack size didn’t seem to have an effect.

I used this code to find the maximum number that can be calculated recursively:

(0...99999).each { |n| fib(n); p "Calculating fib #{n}"; @cache = [0,1] }

]]>
By: Micah Buckley-Farlee http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/#comment-401 Thu, 20 Aug 2015 01:57:00 +0000 http://www.blackbytes.info/?p=2156#comment-401

This will run a lot faster and it will be able to calculate much bigger Fibonacci numbers.

Unfortunately, this is not the case, because of a key limitation in Ruby – the lack of tail call optimization. On my vanilla 2.1.5 build of Ruby, the highest number I can run your recursive, memoized solution of fibs with is 7685 before I get a SystemStackError: stack level too deep

]]>
By: splattael http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/#comment-400 Tue, 18 Aug 2015 11:59:14 +0000 http://www.blackbytes.info/?p=2156#comment-400 I like your solution! It’s very similar to the “hash” version:

]]>
By: Jesus Castello http://www.blackbytes.info/2015/08/ruby-recursion-and-memoization/#comment-399 Mon, 17 Aug 2015 19:15:09 +0000 http://www.blackbytes.info/?p=2156#comment-399 I would say using a hash here doesn’t provide any benefits since we aren’t scanning the array, we are accessing the index directly. That’s a constant time operation, so it doesn’t get much better than that.

]]>