Tutorial by Examples

Let's start with a simple algorithm to see how recursion could be implemented in Ruby. A bakery has products to sell. Products are in packs. It services orders in packs only. Packaging starts from the largest pack size and then the remaining quantities are filled by next pack sizes available. For ...
Many recursive algorithms can be expressed using iteration. For instance, the greatest common denominator function can be written recursively: def gdc (x, y) return x if y == 0 return gdc(y, x%y) end or iteratively: def gdc_iter (x, y) while y != 0 do x, y = y, x%y end re...

Page 1 of 1