The Golden Fibonacci Ratio (#69)

The three rules of Ruby Q.:

  1. Please do not post any solutions or spoiler discussion for this quiz
    until
    48 hours have passed from the time on this message.

  2. Support Ruby Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

  1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Enrique Meza C

You may have noticed that if you have a Golden Rectangle and you cut off
a
square with side lengths equal to the length shorter rectangle side,
then what
remains is another Golden Rectangle.

This could go on forever. You can just keep cutting off these big
squares and
getting smaller and smaller Golden rectangles.

The idea with the Fibonacci series is to do the same thing in reverse.
So the
quiz:

What you do is start with a square (1 by 1), find the longer side, and
add a
square of that size to the whole thing to form a new rectangle.

So when we start with a 1 by 1 square the longest side is one, so we add
another
square to it. Now we have a 2 by 1 rectangle

Then the longest side is two, so we connect a 2 by 2 square to our 2 by
1
rectangle to get a 3 by 2 rectangle. This continues, and the sides of
the
rectangle will always be a successive Fibonacci number. Eventually the
rectangle will be very close to a Golden Rectangle.

I will do a few steps to let you see it in action:

###
# #    1 by 1, so we add 1 by 1 to get...
###

######
# ## #  Now it is 2 by 1, so we add 2 by 2 to get......
######

######
# ## #
######
######
#    #   Now it is 2 by 3, so we add a 3 by 3 to get.......
#    #
#    #
#    #
######

###############
# ## ##       #
#######       #
#    ##       #
#    ##       #  Now it is 3 by 5, so we would add a 5 by 5 square.
#    ##       #
#    ##       #
###############

So the task is visualisation of the Fibonacci series ?

Hi,
the description is really exciting :>
But the main task is for me not clear too.

–verbose please

On Mar 3, 2006, at 8:25 AM, Robert R. wrote:

Hi,
the description is really exciting :>
But the main task is for me not clear too.

Basically the goal is to visualize the sequence. Perhaps you want to
take a parameter and show the blocks that many steps in, or maybe you
want to make it more like geek cinema and watch the sequence progress
block by block…

James Edward G. II

On Mar 3, 2006, at 8:06 AM, Rudolfs O. wrote:

So the task is visualisation of the Fibonacci series ?

Correct. In block format.

James Edward G. II

On Mar 3, 2006, at 11:50 AM, Jacob F. wrote:

block by block…

#

#

#

#

###########################

It might be good to note that multiple layouts are possible. For
example, the above could also spiral as follows:

###########################

#

#

#

#

#

#

#

#

#

# # #

#####

# #

# #

# #

###########################

James Edward G. II

On 3/3/06, James Edward G. II [email protected] wrote:

James Edward G. II

Sample output for my solution, this may be a little clearer than
James’ (still requires monospace font, of course):

$ ruby fibonacci_block.rb 6

###########################

# # #

#

# #

# #

# #

########### #

#

#

#

#

#

#

#

#

#

###########################

Jacob F.

On 3/3/06, James Edward G. II [email protected] wrote:

#

#

#

# # #

#####

# #

# #

# #

###########################

Good point. I actually like that one better, and have altered my code
to produce it. :slight_smile:

Jacob F.

Of course, when rendering large numbers of squares it can be nice to
shrink things a bit:

11:02:56 - tora@Eustacia:~/ruby/fibonacci

ruby gfr.rb 8 1
++±-±------±-------------------+
+++ | | |
| | | | |
±±-+ | |
| | | |
| | | |
| | | |
| | | |
±—±------+ |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
±-----------±-------------------+

or make them larger…

ruby gfr.rb 3 3
±-±-+
| | |
| | |
±-±-+
| |
| |
| |
| |
| |
±----+

(crosses fingers that they render correctly)

Jacob F. wrote:

#

# #

Hi, I’m new to the ruby (and the quiz). I did a quick solution that has
a little cleaner output like so:

±--------------±--------+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| ±±±----+
| | | | |
| ±±+ |
| | | |
| | | |
| | | |
±--------------±–±----+

i guess the next step is to animate it.

Under linux, I have installed ncurses-ruby-1.0, just now and tried
this program.
The rectangles are drawn fairly satisfactorily , but I loose control
on the xterminal, and the bash prompt ovewrites on the rectangles. I
am forced to close the xterminal. I have to still understand the
program.
Prasad

On 3/3/06, Ruby Q. [email protected] wrote:

The idea with the Fibonacci series is to do the same thing in reverse. So the
quiz:

What you do is start with a square (1 by 1), find the longer side, and add a
square of that size to the whole thing to form a new rectangle.

I used this quiz as an excuses to get Ncurses-ruby running on my
Windows box. It was fairly painless, once I found PDCurses. I used
panel borders to draw the boxes, and animated the thing as it grows.
I also added a bunch of command line options for windows:
-resize [x y] will change the window size to fit a bigger rectangle.
-newwin [x y] will launch a new window, then resize (if you don’t want
to mess up your current command window.
I didn’t add the equivalent commands for *nix boxen, it’s been too
long since i used one to remember the right commands.
The program works under windows and cygwin. I’d appreciate it if
someone lets me know if it works ok under linux or mac.

-Adam

#----- goldrect.rb

Submission for Ruby Q. #69

by Adam S.

require ‘ncurses’

class FibBox
Direction=[:up,:left,:down,:right]
@@boxes = []

def self.moveall y,x
@@boxes.each{|f| f.moverel(y,x,true)}
@@boxes.each{|b| b.unhide}
end
def self.animate isize=1
size = [isize,isize]
f = FibBox.new(Direction[dir=2],size,[0,0])
while true
n = size.max
pos = [0,0]
pos[0]+=size.min if dir==2
pos[1]+=size.min if dir==3
n==size[1] ? size[0]+=n : size[1]+=n
break if (size[0] > Ncurses.LINES) || (size[1] > Ncurses.COLS)
f = FibBox.new(Direction[dir],[n,n],pos)
dir=(dir+1)%4
end
end

def initialize dir,size,pos
@size = size
@pos = pos
func = lambda{FibBox.moveall(0,1)} if dir==:left
func = lambda{FibBox.moveall(1,0)} if dir==:up
case dir
when :left, :right
target = @size[1]
parms=[@size[0],1]+@pos
animate=1
when :down, :up
target = @size[0]
parms=[1,@size[1]]+@pos
animate=0
end
func.call if func
win = Ncurses::WINDOW.new(*parms)
@panel=win.new_panel
show
while parms[animate]!=target
sleep(0.01)
parms[animate]+=1
func.call if func
Ncurses::Panel.update_panels
Ncurses.doupdate()
resize(*parms)
end
@@boxes<<self
end

def hide
@panel.window.border(*([’ '[0]]8))
end
def unhide
@panel.window.border(
([0]8))
end
def show
unhide
Ncurses::Panel.update_panels
Ncurses.doupdate()
sleep(0.1/@size[0]) #sleep less when box takes longer to draw
end
def resize(sy,sx,ly,lx)
nw=Ncurses::WINDOW.new(sy,sx,ly,lx)
exit(0) if !nw
w = @panel.window
@panel.replace(nw)
w.delete
show
end
def moverel(dy,dx,keephidden=false)
hide
@pos[0]+=dy
@pos[1]+=dx
@panel.move(
@pos)
show unless keephidden
end
end

if FILE == $0
if (ARGV[0].to_i)>0
size = ARGV.shift.to_i
end
if RUBY_PLATFORM =~ /mswin/
case ARGV[0]
when ‘-h’, ‘-?’
puts “usage: #{$0} [size] [-newwin||-resize] [x y]”
puts " size: initial box size (default 1)"
puts " -resize: resizes your screen to x,y (default 150x90)"
puts " -newwin: launches program in a new resized window"
exit
when ‘-newwin’
start /WAIT ruby #{$0} #{size||1} -resize #{ARGV[1]} #{ARGV[2]}
exit
when ‘-resize’
mode con cols=#{ARGV[1]||150} lines=#{ARGV[2]||90}
end
end
Ncurses.initscr
Ncurses.noecho
FibBox.animate size||1
Ncurses.stdscr.getch
end

El jue, 09-03-2006 a las 01:35 +0900, G.Durga P. escribió:

Under linux, I have installed ncurses-ruby-1.0, just now and tried
this program.
The rectangles are drawn fairly satisfactorily , but I loose control
on the xterminal, and the bash prompt ovewrites on the rectangles. I
am forced to close the xterminal. I have to still understand the
program.
Prasad

Use ‘#tset -s’ to reset the xterminal.

I tried it and works fine.

On 3/8/06, Enrique Meza C [email protected] wrote:

El jue, 09-03-2006 a las 01:35 +0900, G.Durga P. escribió:

Under linux, I have installed ncurses-ruby-1.0, just now and tried
this program.
The rectangles are drawn fairly satisfactorily ,

I forgot to mention that it looks best if you can set your terminal to
a square font, like Terminal 8x8 under Windows.

but I loose control
on the xterminal, and the bash prompt ovewrites on the rectangles. I
am forced to close the xterminal. I have to still understand the
program.
Prasad

Use ‘#tset -s’ to reset the xterminal.

When should that be issued? Should the ruby script call it before it
exits?

I tried it and works fine.

Cool. Thanks for checking.