#!/usr/bin/ruby # # Copyright (c) 2005 Thomer M. Gil # http://thomer.com/ # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 59 Temple # Place - Suite 330, Boston, MA 02111-1307, USA. # # # This little program visualizes the following riddle: You have 55 matches # arranged in some number of piles of different sizes. You now do the # following operation: pick one match from each pile, and form a new pile. You # repeat this ad infinitum. What is the steady state? Is it unique? See also # http://thomer.com/riddles/ # require 'pp' # # Start out with one heap of 55 matches. You can change this to whatever you # want. For example, for 3 piles, you could do: # # heaps = [10, 20, 25] # heaps = [55] i = -1 while true print "#{i += 1} " pp heaps nheaps = [] heaps.each do |x| nheaps << (x-1) if x > 1 end (nheaps << heaps.length).sort! break if heaps == nheaps heaps = nheaps.clone end